python更改图片后缀名为jpg格式
1 import os 2 import cv2 3 class GeneDataset(): 4 def __init__(self,rootdir): 5 self.rootdir = rootdir 6 self.subdirs = [] 7 def looksubdir(self): 8 list_dirs = os.walk(self.rootdir) 9 for root, dirs, files in list_dirs: 10 for d in dirs: 11 self.subdirs.append(os.path.join(root, d)) 12 print("subdir =", os.path.join(root, d)) 13 def reformat(self):14 for subdir in self.subdirs: 15 list_dirs = os.walk(subdir) 16 for root, dirs, files in list_dirs: 17 for f in files: 18 srcname = os.path.join(root, f) 19 print('srcname', srcname) 20 srcformat = srcname.split('.')[-1] 21 if srcformat is not '.jpg': 22 img = cv2.imread(srcname) 23 newname = srcname.replace(srcformat, 'jpg') 24 print('newname=', newname) 25 cv2.imwrite(newname, img) 26 os.remove(srcname) 27 path = r'E:\data\pic' 28 myclassdataset = GeneDataset(path) 29 myclassdataset.looksubdir() 30 myclassdataset.reformat()