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()
-