图片文件重新从1.jpg开始改名

folder_path = r'C:\Users\23608\Desktop\Luli_work\data\fanStudent\tongueseg\Fan\Fan\.jpg'
new_folder = r'C:\Users\23608\Desktop\Luli_work\data\fanStudent\tongueseg\imgOrig'

jpg_files = [f for f in os.listdir(folder_path) if f.endswith('.jpg')]
n = 1
for i, jpg_file in enumerate(jpg_files):
    new_filename = f'{n}.jpg'
    n =n+1
    
    # 构造原始文件和新文件的完整路径
    original_path = os.path.join(folder_path, jpg_file)
    new_path = os.path.join(new_folder, new_filename)
    
    # 复制文件到新文件夹并重命名
    shutil.copy(original_path, new_path)
    
print("重命名完成!")

import os

def rename_images_and_txt(folder_path):
    # 获取文件夹下所有文件
    files = os.listdir(folder_path)
    
    # 过滤出.jpg和.BMP文件
    image_files = [f for f in files if f.lower().endswith(('.jpg', '.bmp'))]

    # 对每张图片进行重新命名
    for index, image_file in enumerate(image_files, start=1):

        base_name = image_file.split('.')[1]
        # 构建新的文件名
        new_image_name = f"{index}.{base_name}"
        new_txt_name = f"{index}.txt"
        
        # 构建旧文件的完整路径
        old_image_path = os.path.join(folder_path, image_file)
        old_txt_path = os.path.join(folder_path, f"{os.path.splitext(image_file)[0]}.txt")
        
        # 构建新文件的完整路径
        new_image_path = os.path.join(folder_path, new_image_name)
        new_txt_path = os.path.join(folder_path, new_txt_name)
        
        # 重命名图片文件
        os.rename(old_image_path, new_image_path)
        
        # 重命名对应的txt文件
        os.rename(old_txt_path, new_txt_path)

if __name__ == "__main__":
    folder_path = "/share1/luli/yolov8/dataset/throats_data/obj_Train_data"
    rename_images_and_txt(folder_path)

import os
from PIL import Image

image_folder_path = r'C:\Users\23608\Desktop\Luli_work\data\fanStudent\dataset1\rename_img_labels\images'
label_folder_path = r'C:\Users\23608\Desktop\Luli_work\data\fanStudent\dataset1\rename_img_labels\labels'

new_image_path = r'C:\Users\23608\Desktop\Luli_work\data\fanStudent\dataset1\rename_img_labels\new_images'
new_label_path = r'C:\Users\23608\Desktop\Luli_work\data\fanStudent\dataset1\rename_img_labels\new_labels'
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、付费专栏及课程。

余额充值