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!')
运行结果: