您好,登錄后才能下訂單哦!
//有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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。