批量图片重命名(python版)

参考:https://blog.csdn.net/m0_37592397/article/details/80372009

# -*- coding:utf8 -*-
import os

class BatchRename():
    """
    批量重命名文件夹中的图片文件
    """
    def __init__(self):
        self.path='/home/xu/caffe/data/mydata/test_neg'  #表示需要命名处理的文件夹
        
    def rename(self):
        filelist=os.listdir(self.path)  #获取文件路径
        total_num=len(filelist)  #获取文件长度(个数)
        i=1  #表示文件的命名是从1开始的
        for item in filelist:
            if item.endswith('.jpg'):  #转换格式就可以调整为自己需要的格式即可
                src=os.path.join(os.path.abspath(self.path),item)
                dst=os.path.join(os.path.abspath(self.path),''+str(i)+'.jpg')
                #dst = os.path.join(os.path.abspath(self.path), '0000' + format(str(i), '0>3s') + '.jpg')    
                #这种情况下的命名格式为0000000.jpg形式,可以自定义格式
                try:
                    os.rename(src,dst)
                    print('converting %s to %s ...' % (src, dst))
                    i=i+1
                except:
                    continue
        print('total %d to rename & converted %d jpgs' % (total_num, i))
        
if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()

猜你喜欢

转载自blog.csdn.net/qq_18644873/article/details/84636279
今日推荐