python 对文件中的图像进行批处理重命名、改格式。
记录一下常用的代码,对文件夹中的图像进行重命名个格式格式转化。直接上代码
import os class BatchRename(): ''' 批量重命名文件夹中的图片文件 ''' def __init__(self): self.path = 'F:/图像去雾项目/含雾图像集' def rename(self): filelist = os.listdir(self.path) total_num = len(filelist) print(total_num) i = 0 for item in filelist: print(item) if item.endswith('.tif'):# src = os.path.join(os.path.abspath(self.path), item) dst = os.path.join(os.path.abspath(self.path), str(i) +'img'+ '.jpg') print(item ,i) try: os.rename(src, dst) print ("converting %s to %s ..." % (src, dst)) i = i + 1 except: continue print ("total %d to rename & converted %d jpg" % (total_num, i)) if __name__ == '__main__': demo = BatchRename() demo.rename()

浙公网安备 33010602011771号