//有1-22个文件夹,各文件夹下有Detect_0文件夹,此文件夹下有source与mask文件夹,目的是将需要获取图片的
文件夹下的图片复制到新的文件夹下并按顺序重命名
import os import shutil //删除之前文件夹并新建空文件夹 shutil.rmtree(r'E:\\all_project\\picture') os.makedirs("E:\\all_project\\picture\\source\\") os.makedirs("E:\\all_project\\picture\\mask\\") //确定需要获取图片的文件夹编号 # arr=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22"] arr=["1","2"] source_num=0 mask_num=0 source_new = 1 mask_new = 1 for i in arr: source = 'E:\\all_project\\'+i+'\\Detect_0\\source\\' source_final = 'E:\\all_project\\picture\\source\\' source_ls = os.listdir(source) print(source_ls) mask = 'E:\\all_project\\'+i+'\\Detect_0\\mask\\' mask_final = 'E:\\all_project\\picture\\mask\\' mask_ls = os.listdir(mask) print(mask_ls) source_list = os.listdir(source) mask_list = os.listdir(mask) source_num += len(source_ls) mask_num += len(mask_ls) print(source_num) print(mask_num) old = 1 while len(os.listdir(source_final)) < source_num: oldname = source + str(old) + ".bmp" if os.path.exists(oldname): newname = source_final + str(source_new) + ".bmp" shutil.copyfile(oldname, newname) source_new += 1 print(source_new) old += 1 else: old += 1 old = 1 while len(os.listdir(mask_final)) < mask_num: oldname = mask + str(old) + ".bmp" if os.path.exists(oldname): newname = mask_final + str(mask_new) + ".bmp" shutil.copyfile(oldname, newname) mask_new += 1 old += 1 else: old += 1
以上这篇python实现本地图片转存并重命名的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。