对图像数据进行重命名

对图像数据进行重命名,第1张

对图像数据进行重命名
# coding=utf-8
import os  # 打开文件时需要


class BatchRename():
    def __init__(self):
        # 我的图片文件夹路径
        #self.path = 'C:/Users/Lenovo/Desktop/VOCdevkit/JPEGImages'
        self.path = 'C:/Users/Lenovo/Desktop/VOCdevkit/JPEGImages'

    def rename(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)
        i = 34 # 图片编号从多少开始,不要跟其他人原本的编号重复了
        n = 6
        for item in filelist:
            if item.endswith('.jpg'):
                n = 6 - len(str(i))
                src = os.path.join(os.path.abspath(self.path), item)
                dst = os.path.join(os.path.abspath(self.path), str(0)*n+str(i) + '.jpg')
                try:
                    os.rename(src, dst)
                    i = i + 1
                except:
                    continue


if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5721488.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-18
下一篇2022-12-18

发表评论

登录后才能评论

评论列表(0条)

    保存