您当前的位置:首页 > 计算机 > 编程开发 > Python

python获取路径下图片名称并读出写入

时间:02-03来源:作者:点击数:

python获取路径下图片名称并读出写入

# -*- coding: utf-8 -*-

import os
import cv2
import sys

def get_img_name():
    imglist = os.listdir('/home/xxx/thirdparty/labelImg-master/data/img')
    imgnamefile = '/home/xxx/thirdparty/labelImg-master/data/imgnamefile.txt'
    if not os.path.exists(imgnamefile):
        imgnamefile = open(imgnamefile, 'w')
        for i in imglist:
            img_name = i.split('.')[0] + '.' + i.split('.')[1] + '.png'
            imgnamefile.write(img_name + "\n")
            # imgnamefile.writelines(img_name)
        imgnamefile.close()


def flip_img():
    fin = open('/home/xxx/thirdparty/labelImg-master/data/imgnamefile.txt', 'r')
    outimgdir = '/home/xxx/thirdparty/labelImg-master/data/out_img_dir/'
    if not os.path.exists(outimgdir):
        os.makedirs(outimgdir)
    os.system('cd /home/xxx/thirdparty/labelImg-master/data/img')
    for name in fin.readlines():
        name = (name.split('.')[0] + '.' + name.split('.')[1] + '.' + name.split('.')[2]).strip("\n")
        # imgpath = '/home/xxx/thirdparty/labelImg-master/data/img/' + name
        srcimgpath = os.path.join('/home/xxx/thirdparty/labelImg-master/data/img',name)
        print(srcimgpath)
        src = cv2.imread(srcimgpath)
        # cv2.imshow('src',src)
        dst = cv2.flip(src, -1)
        # cv2.imshow("dst",dst)
        # cv2.waitKey(0.)
        outimgpath = os.path.join(outimgdir,name)
        cv2.imwrite(outimgpath, dst)

    fin.close()

if __name__=='__main__':
    get_img_name()
    flip_img()
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门