将图片和对应的标签文文件重新命名

文章描述了使用Python的os模块对图像文件和其对应的标签文件进行批量重命名和移动的示例,包括针对不同类型的图片(jpg,png)的操作,以及处理可能存在的不存在的标签文件情况。
摘要由CSDN通过智能技术生成
import os

# 指定文件夹路径
image_folder_path = r'path\\to\\images'
label_folder_path = r'path\\to\\labels'

# 获取前10张图片的文件名
image_files = os.listdir(image_folder_path)

# 遍历每张图片
for i, image_file in enumerate(image_files):
    if image_file.endswith('.jpg'):
        # 构建图片和对应标签文件的路径
        image_path = os.path.join(image_folder_path, image_file)
        new_image_name = f"{i + 1}.jpg"
        new_image_path = os.path.join(image_folder_path, new_image_name)
        
        # 重命名图片文件
        os.rename(image_path, new_image_path)

        label_file = os.path.join(label_folder_path, os.path.splitext(image_file)[0] + '.txt')
        new_label_name = f"{i + 1}.txt"
        new_label_file = os.path.join(label_folder_path, new_label_name)
        
        # 重命名对应的标签文件
        os.rename(label_file, new_label_file)

        print(f"Renamed {image_file} to {new_image_name} and {os.path.splitext(image_file)[0]+'.txt'} to {new_label_name}")

在这里插入图片描述

import os
# 指定文件夹路径

# image_folder_path = r'C:\\Users\\23608\\Desktop\\Luli_work\\eyes\\eye detection.v4i.yolov8\\test\\images'
# label_folder_path = r'C:\\Users\\23608\\Desktop\\Luli_work\\eyes\\eye detection.v4i.yolov8\\test\\labels'
imgs = r'C:\Users\23608\Documents\WeChat Files\wxid_xshticce87f311\FileStorage\File\2024-01\sorted(2)\sorted\raw'
image_folder_path = imgs
txt = r'C:\Users\23608\Documents\WeChat Files\wxid_xshticce87f311\FileStorage\File\2024-01\sorted(2)\sorted\txt_dir'
label_folder_path = txt
# C:\Users\23608\Desktop\test\labels
# 获取前10张图片的文件名
image_files = os.listdir(image_folder_path)

# 遍历每张图片
n = 0
for i, image_file in enumerate(image_files):
    if image_file.endswith('.jpg') :
        
        base_name = os.path.splitext(image_file)[0]  # 获取文件名(不包含扩展名的部分)
        new_base_name = base_name + '-jpg' # 在文件名后添加 '-png'
        label_file = os.path.join(label_folder_path, new_base_name + '.txt')
        
        if os.path.exists(label_file):
            n = n+1
            # 构建图片和对应标签文件的路径
            image_path = os.path.join(image_folder_path, image_file)
            new_image_name = f"{n}.jpg"
            new_image_path = os.path.join(image_folder_path, new_image_name)
            # 重命名图片文件
            os.rename(image_path, new_image_path)           
            
            new_label_name = f"{n}.txt"
            new_label_file = os.path.join(label_folder_path, new_label_name)
            # 重命名对应的标签文件
            os.rename(label_file, new_label_file)
            print(f"Renamed {image_file} to {new_image_name} and {new_base_name+'.txt'} to {new_label_name}")
        
        else:
            print(f"jpg {base_name} not exist!")
        
    else image_file.endswith('.png') :
        base_name = os.path.splitext(image_file)[0]  # 获取文件名(不包含扩展名的部分)
        new_base_name = base_name + '-png'  # 在文件名后添加 '-png'
        label_file = os.path.join(label_folder_path, new_base_name + '.txt')

        if os.path.exists(label_file):
            n = n+1
            image_path = os.path.join(image_folder_path, image_file)
            new_image_name = f"{n}.png"
            new_image_path = os.path.join(image_folder_path, new_image_name)
            # 重命名图片文件
            os.rename(image_path, new_image_path)

            new_label_name = f"{n}.txt"
            new_label_file = os.path.join(label_folder_path, new_label_name)
            # 重命名对应的标签文件
            os.rename(label_file, new_label_file)
            print(f"Renamed {image_file} to {new_image_name} and {new_base_name+'.txt'} to {new_label_name}")
        
        else:
            print(f"png {base_name} not exist!")
                

中间存在没有boxes的直接跳过,保证重新命名的图片名换是一致的。
在这里插入图片描述

import os
from PIL import Image

image_folder_path = r'path\to\dataset1\test\images'
label_folder_path = r'path\to\dataset1\test\labels'

new_image_path = r'path\to\dataset1\test\new_images'
new_label_path = r'path\to\test\new_labels'
'''
# 这个步骤很重要,不然这个代码只能执行第一张图片之后就报错FileNotFoundError:
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'C:\\path\\to\\dataset1\\test\\images\\0001.jpg' -> 'C:\\path\\to\\dataset1\\test\\new_images\\1.jpg\\2.jpg'
可以看到这个地方是在读取文件夹里面第二张图片的时候试着想把test\\images\\0001.jpg复制到\\test\\new_images\\1.jpg\\2.jpg',这个的路径里面多了一个\\1.jpg,也就是说它把刚刚第一个图片的名称一起加进来了。

是因为os.path.join会在原本的路径上修改(具体原因再看看)
'''
new_img_path = new_image_path 

if not os.path.exists(new_image_path):
    os.makedirs(new_image_path)
if not os.path.exists(new_label_path):
    os.makedirs(new_label_path)

# 获取图片的名称
image_files = [f for f in os.listdir(image_folder_path) if f.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp'))]
n = 0

for i, image_file in enumerate(image_files):
    base_name,hz_name = os.path.splitext(image_file)  # 获取文件名(不包含扩展名的部分)
    label_file = os.path.join(label_folder_path,base_name + '.png')
    if os.path.exists(label_file):
        n = n+1
        new_image_name = f"{n}{hz_name}"
        old_image_path = os.path.join(image_folder_path, image_file)
        print(old_image_path)
        new_image_path = os.path.join(new_img_path, new_image_name)
        print(new_image_path)
        os.rename(old_image_path, new_image_path)     
        
        print(base_name)
        label_name = f'{base_name}.png'
        new_label_name = f"{n}.png"
        old_label_path = os.path.join(label_folder_path, label_name)
        print(old_label_path)
        new_label_file = os.path.join(new_label_path, new_label_name)
        os.rename(old_label_path, new_label_file)
        
        print(f'Renamed finished!')

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值