import os
import shutil
from test import arr_name
path ='D:\\Postgraduate\\Chinese Traffic Signs_datasets\\Traffic_Signs'
new_path ='D:\\Postgraduate\\Chinese Traffic Signs_datasets\\Traffic_Signs_Classify1'
filelist = os.listdir(path)
arr = arr_name(58)for i inrange(1,59):
os.mkdir((new_path+'\\ts{}').format(i))
j =0# 文件名for item in filelist:# 从原路径下选取图片
i =1
j = j +1# if item.endswith('5.bmp') or item.endswith('6.bmp') or item.endswith('7.bmp') or item.endswith('8.bmp'):for x in arr:# 把文件分为58类if item[0:3]== x:
src = os.path.join(os.path.abspath(path), item)#
dst = os.path.join(os.path.abspath((new_path+'\\ts{}').format(i)), item)#
new_name = os.path.join(os.path.abspath((new_path+'\\ts{}').format(i)),''+str(j)+'.jpg')# 复制图像
shutil.copy(src, dst)# 重命名
os.rename(dst, new_name)print(src)# 输出原图像名print(new_name)# 输出新图像名
i +=1
test.py
用于区分第10类以后与前10类的命名区别(0xx <–> 0xx)
defarr_name(max):
numes =list()for i inrange(max):if i <10:
numes.append('00'+str(i))elif10<= i <100:
numes.append('0'+str(i))else:
numes.append(str(i))return numes