自制转换脚本

该代码段使用Python的os和shutil模块实现批量重命名图片文件和对应的文本文件。当文件名中包含-时,从该位置截取新的文件名。脚本首先遍历指定路径下的图像和文本文件,然后根据规则进行重命名并复制到新的目录。
摘要由CSDN通过智能技术生成
import os
import shutil
# by 老猫

# 图片文本修改
def rename_file(img_path, new_img, txt_path, new_txt):
    # 打开源文件图像
    file = os.listdir(img_path)
    for i in range(len(file)):
        # 获得图像扩展名
        (name, extent) = os.path.splitext(file[i])
        print(name, end='')
        print(extent)
        # 获得图像对应的txt文件
        txt_file = os.path.join(txt_path, name + '.txt')

        for j in range(len(name)):
            if name[j] == '-':
                flag = 1
                break
        n = name[j:]

        # 源文件
        src = os.path.join(img_path, str(file[i]))
        a = 1 + i

        if flag == 1:
            shutil.copy(src, os.path.join(new_img, '%03d'%int(a) + n + '.jpg'))
            shutil.copy(txt_file, os.path.join(new_txt, '%03d'%int(a) + n + '.txt'))
            print('%03d'%int(a) + n + '.jpg')
            print('%03d'%int(a) + n + '.txt')
            print('---------------------------------------------')
        else:
            shutil.copy(src, os.path.join(new_img, '%03d'%int(a) + '.jpg'))
            shutil.copy(txt_file, os.path.join(new_txt, '%03d'%int(a) + '.txt'))
            print('%03d'%int(a) + '.jpg')
            print('%03d'%int(a) + '.txt')
            print('---------------------------------------------')





# 图片修改
def rename_img(img_path, new_img):
    # 打开源文件图像
    file = os.listdir(img_path)
    for i in range(len(file)):
        flag = 0
        # 获得图像扩展名
        [name, extent] = os.path.splitext(file[i])
        print(name, end='')
        print(extent)
        for j in range(len(name)):
            if name[j] == '-':
                flag = 1
                break
        n =name[j:]
        # 源文件
        src = os.path.join(img_path, str(file[i]))
        a = 1 + i

        if flag == 1:
            shutil.copy(src, os.path.join(new_img, '%03d'%int(a) + n + '.jpg'))
            print('%03d'%int(a) + n + '.jpg')
            print('---------------------------------------------')
        else:
            shutil.copy(src, os.path.join(new_img, '%03d'%int(a) + '.jpg'))
            print('%03d'%int(a)  + '.jpg')
            print('---------------------------------------------')


# 文本修改
def rename_txt(txt_path, new_txt):
    # 打开源文件图像
    file = os.listdir(txt_path)
    for i in range(len(file)):
        flag = 0
        # 获得图像扩展名
        [name, extent] = os.path.splitext(file[i])
        print(name, end='')
        print(extent)
        for j in range(len(name)):
            if name[j] == '-':
                flag = 1
                break
        n =name[j:]
        # 源文件
        src = os.path.join(txt_path, str(file[i]))
        a = 1 + i

        if flag == 1:
            shutil.copy(src, os.path.join(new_txt, '%03d'%int(a) + n + '.txt'))
            print('%03d'%int(a) + n + '.txt')
            print('---------------------------------------------')
        else:
            shutil.copy(src, os.path.join(new_txt, '%03d'%int(a) + '.txt'))
            print('%03d'%int(a) + '.txt')
            print('---------------------------------------------')


if __name__ == "__main__":

    img_path = r'C:\Users\back to life\Desktop\myflax\images\train'
    new_img = r'C:\Users\back to life\Desktop\myflax\img'
    txt_path = r'C:\Users\back to life\Desktop\myflax\labels\train'
    new_txt = r'C:\Users\back to life\Desktop\myflax\lab'

# ctrl-f select all  %03d - %0id

    # rename_img(img_path, new_img)
    # rename_txt(txt_path, new_txt)
    rename_file(img_path, new_img, txt_path, new_txt)



# 图像修改运行

      # 原图片文件夹路径
#     img_path = r'C:\Users\back to life\Desktop\demotest'
      # 新图片文件夹路径
#     new_img = r'C:\Users\back to life\Desktop\ans'

#     rename_img(img_path, new_img)


# 文本修改运行

      # 原文本文件夹路径
#     txt_path = r'C:\Users\back to life\Desktop\demotest'
      # 原文本文件夹路径
#     new_txt = r'C:\Users\back to life\Desktop\ans'

#     rename_txt(txt_path, new_txt)
    print('-------------finish--------------------------')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值