import os
class BatchRename():
def __init__(self):
self.path = r'image-1\test-1\yellow' # 图片的路径
def rename(self):
filelist = os.listdir(self.path)
filelist.sort()
total_num = len(filelist) # 获取文件中有多少图片
i = 100 # 文件命名从哪里开始(即命名从哪里开始)
for item in filelist:
if item.endswith('.png'):
src = os.path.join(self.path, item)
dst = os.path.join(os.path.abspath(self.path), 'yellow.' + str(i) + '.png')
try:
os.rename(src, dst)
print('converting %s to %s ...' % (src, dst))
i = i + 1
except Exception as e:
print(e)
print('rename dir fail\r\n')
print('total %d to rename & converted %d jpgs' % (total_num, i))
if __name__ == '__main__':
demo = BatchRename() # 创建对象
demo.rename() # 调用对象的方法
使用python给图片批量命名
最新推荐文章于 2023-10-16 08:41:02 发布