import os
class ImageRename():
def __init__(self):
self.path = 'C:/Users/lenovo/Desktop/imglabel/0.6-0.8'
def rename(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
i = 0
for item in filelist:
if item.endswith('.png'):
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), format(str(i), '0>2s') + '.png')
os.rename(src, dst)
print ('converting %s to %s ...' % (src, dst))
i = i + 1
if item.endswith('.jpg'):
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), format(str(i), '0>2s') + '.jpg')
os.rename(src, dst)
print ('converting %s to %s ...' % (src, dst))
i = i + 1
if item.endswith('.JPG'):
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), format(str(i), '0>2s') + '.JPG')
os.rename(src, dst)
print ('converting %s to %s ...' % (src, dst))
i = i + 1
print ('total %d to rename & converted %d jpgs' % (total_num, i))
if __name__ == '__main__':
newname = ImageRename()
newname.rename()
图片文件批量重命名(Python)
最新推荐文章于 2023-03-14 19:14:21 发布