import os
import shutil
# 定义文件夹路径
folder1 = 'D:\Desktop\PSACAL VOC2010\VOCdevkit\VOC2010\JPEGImages'
folder2 = 'D:\Desktop\pascal_person_part\pascal_person_part\pascal_person_part_gt'
output_folder = 'D:\Desktop\pascal_person_part_img'
# 获取文件夹2中的png图片文件名列表
folder2_files = [f for f in os.listdir(folder2) if os.path.isfile(os.path.join(folder2, f)) and f.endswith('.png')]
# 在文件夹1中查找文件名与文件夹2中png图片文件名相同的jpg图片,并复制到新路径
for file2 in folder2_files:
jpg_file = file2.replace('.png', '.jpg')
file_path1 = os.path.join(folder1, jpg_file)
output_file_path = os.path.join(output_folder, jpg_file)
if os.path.exists(file_path1):
shutil.copyfile(file_path1, output_file_path)
print("复制完成!")
在文件夹1中查找文件名与文件夹2中图片文件名相同的图片,并复制到新路径
于 2024-06-27 16:43:33 首次发布