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)
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)
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!')