实现批量图片的重命名
# -*- coding: utf-8 -*-
# @Time : 2022/6/23 9:50
# @Author : lc
# @File : batch_rename.py
# @Software: PyCharm
# @Function: Inplement batch image renaming
import os
def rename(img_dir):
order = 0
filelist = os.listdir(img_dir)
print("Total number of images: {}".format(len(filelist)))
for file in sorted(filelist):
img_path = os.path.join(img_dir, file)
prefix = str(order).zfill(3)
suffix = os.path.splitext(file)[1]
save_path = img_dir + "new" + prefix + suffix
# save_path = os.path.join(img_dir, prefix + suffix)
os.rename(img_path, save_path)
old_name = img_path.split('/')[-1]
new_name = save_path.split('/')[-1]
print("Old Name: {} New Name: {}".format(old_name, new_name))
order += 1
print("Completed!")
if __name__ == "__main__":
img_dir = r"./images/"
rename(img_dir)