图片批量重命名,python

#原始版本 ----不懂事的时候写的
import os
class ImageRename():
    def __init__(self):
        self.path = '/home/aaa/caffe/data/myself/11/train/tomato'

    def rename(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)
        i = 0
        for item in filelist:
            if item.endswith('.jpeg'):
                src = os.path.join(os.path.abspath(self.path), item)
                #dst = os.path.join(os.path.abspath(self.path), '0000' + format(str(i), '0>3s') + '.jpg')
        dst = os.path.join(os.path.abspath(self.path),format(str(i)) + '.jpeg')
                os.rename(src, dst)
                print 'converting %s to %s ...' % (src, dst)
                i = i + 1
        print 'total %d to rename & converted %d jpegs' % (total_num, i)

if __name__ == '__main__':
    newname = ImageRename()
    newname.rename()

更精炼的版本

#后来写的
import os

# 图片存放的路径
path = r"E:/datasets/yanye_shixiong/classify/val/1"

# 遍历更改文件名
num = 1
for file in os.listdir(path):
    os.rename(os.path.join(path,file),os.path.join(path,str(num))+".png")
    num = num + 1

 

你可能感兴趣的:(Ubuntu)