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--------------------------')
自制转换脚本
于 2023-04-15 15:38:34 首次发布
该代码段使用Python的os和shutil模块实现批量重命名图片文件和对应的文本文件。当文件名中包含-时,从该位置截取新的文件名。脚本首先遍历指定路径下的图像和文本文件,然后根据规则进行重命名并复制到新的目录。
摘要由CSDN通过智能技术生成