Q:
进行数据集图片预处理时,初始命名如下图(Fig1左),发现读取文件时,读取的结构并非如所设想的那样顺序读取
Fig 1
A:
pyhton读取文件的时候,按照文件名的ascii码中的顺序进行逐位排序,于是编写以下代码,更改命名规则,程序如下
1 # -*- coding:utf-8 -*- 2 # ------------------------------ 3 # @Time :2019/5/26 10:55 4 # @Author :jonie 5 # @Email :[email protected] 6 # @File :重命名.py 7 # Description:实现形如"类名_000x.jpg"的命名方式 8 # ------------------------------ 9 import os 10 def re_name(path_name): 11 num = 0 12 for dir_image in os.listdir(path_name): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表 13 # print(dir_image) 14 full_path = os.path.abspath(os.path.join(path_name, dir_image)) 15 # print(full_path) 16 17 if os.path.isdir(full_path): # 如果是文件夹,继续递归调用 18 if not os.path.exists(os.path.join(r"D:\Jonie_Project_scc15\dataset\scene_categories",dir_image)): 19 os.makedirs(os.path.join(r"D:\Jonie_Project_scc15\dataset\scene_categories",dir_image)) 20 # print(os.path.join(r"D:\Jonie_Project_ot8\data1\scene_categories",dir_image)) 21 print("isdir") 22 re_name(full_path) 23 # print(full_path) 24 else: # 如果是文件了 25 # pass 26 # print(dir_image) 27 # print(full_path) 28 if dir_image.endswith(‘.jpg‘): 29 cla=full_path.split(‘\\‘)[-2] 30 image_name = ‘%s_%04d.jpg‘ % (cla, num) # 注意这里图片名一定要加上扩展名,否则后面imwrite的时候会报错 31 image_name =os.path.join("D:\Jonie_Project_scc15","dataset\scene_categories",cla,image_name) 32 oldname=os.path.join("D:\Jonie_Project_scc15","dataset\scene_categories",cla,image_name) 33 # print(image_name) 34 print(full_path) 35 os.rename(full_path, image_name) 36 num = num + 1 37 if __name__ == ‘__main__‘: 38 re_name(r"D:\Jonie_Project_scc15\dataset\scene_categories")
rename.py
再次进行读取,发现达到预期结果
Fig 2
原文地址:https://www.cnblogs.com/jqpy1994/p/10925459.html
时间: 2024-11-06 19:38:30