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

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}")

将图片和对应的标签文文件重新命名_第1张图片

你可能感兴趣的:(实习记录,实用,python)