DDR爱好者之家 Design By 杰米

//有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实现本地图片转存并重命名的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米