批量更改数据集中图像的文件名
数据集的格式如下:
数据集名称(如My_ImageSet):
----类别1
--------image_1
--------image_2
--------......
----类别2
--------image_1
--------image_2
以此类推
批量更改图像文件名的代码如下:
# _*_ coding: UTF-8 _*_ # Author: liming import os import re import sys data_dir = os.getcwd() + '\\' + 'My-Scene' folder_list = os.listdir(data_dir) folder_num = len(folder_list) for folder_name in folder_list: # 当前图像文件夹名称 print('\n当前场景文件夹名字为: %s\n' % folder_name) image_list = os.listdir(data_dir + '\\' + folder_name) image_num = len(image_list) num = 1 for image_name in image_list: #print('当前场景图像的名字为: %s' % image_name) old_name = data_dir + '\\' + folder_name + '\\' + image_name a = image_name[:-4] new_name = data_dir + '\\' + folder_name + '\\' + folder_name + '_' + str(num) + '.jpg' os.rename(old_name, new_name) num += 1 print('文件夹%s中的图像已更名完毕.' % folder_name) print('---------------------------------------') print('所有文件夹的图像重命名完毕.')
更改后的结果如下: