直接对train里面的目录操作,同级建立一个train1接受处理好的图片
import os
from PIL import Image
def clean(w=64,h=64):
path = "D:/rubbish_perfect/test"
save_path = "D:/rubbish_perfect/test1"
if not os.path.exists(save_path):
os.mkdir(save_path)
file_names = os.listdir(path) # 获取路径下所有文件的名字
for file_name in file_names:
lower_directory=path+"/"+str(file_name)
save_ds=save_path+"/"+str(file_name)
if not os.path.exists(save_ds):
os.mkdir(save_ds)
lower_directory_names=os.listdir(lower_directory)
for lower_directory_name in lower_directory_names:
photo_path=lower_directory+"/"+str(lower_directory_name)
# print(photo_path)
save_name = save_path + "/" +str(file_name) +"/"+str(lower_directory_name)
#print(save_name)
try:
pic = Image.open(photo_path)
pic = pic.resize((w, h))
pic.save(save_name)
print("成功")
except:
print("fail")
if __name__ == '__main__':
clean()
直接把图片全部处理完成,目录结构也是相同的