图片数据集有序重命名python脚本

# 人员:狮子王
# 开发时间:2022/11/22 15:28
# 开始执行程序前,首先把对应文件夹的照片全选,然后右键重命名,改为0.这一操作是为了防止报错
import os


class Rename:
    def __init__(self):
        self.path = "F:\JPEGImages"  # 图片所存放的路径

    def rename(self):
        filelist = os.listdir(self.path)
        print(filelist)
        total_num = len(filelist)
        print(total_num)
        i = 1

        for n in filelist:  # 常见的三种照片存放格式
            if n.endswith('.jpg'):
                src = os.path.join(os.path.abspath(self.path), n)
                dst = os.path.join(os.path.abspath(self.path), "mask_" + str(i) + ".jpg")
            if n.endswith('.png'):
                src = os.path.join(os.path.abspath(self.path), n)
                dst = os.path.join(os.path.abspath(self.path), "mask_" + str(i) + ".png")
            if n.endswith(".jpeg"):
                src = os.path.join(os.path.abspath(self.path), n)
                dst = os.path.join(os.path.abspath(self.path), "mask_" + str(i) + ".jpeg")
            os.rename(src, dst)
            i = i + 1

        print("total %d to rename & converted %d pictures" % (total_num, i))


if __name__ == '__main__':
    exp = Rename()
    exp.rename()

你可能感兴趣的:(人工智能,python)