[Python] 纯文本查看 复制代码 import os
import shutil
import itertools
# 源文件夹路径
source_folders = {
'yellow': 'path/to/yellow_folder',
'white': 'path/to/white_folder',
'green': 'path/to/green_folder',
'black': 'path/to/black_folder'
}
# 目标文件夹的基础路径
target_base_path = 'path/to/target_folder'
# 确保目标文件夹存在
if not os.path.exists(target_base_path):
os.makedirs(target_base_path)
# 获取每个文件夹的图片列表
image_lists = {color: os.listdir(folder) for color, folder in source_folders.items()}
# 检查每个文件夹是否都有500张图片
for color, images in image_lists.items():
if len(images) != 500:
raise ValueError(f"Folder {color} does not contain 500 images.")
# 生成500个不同的图片组合
for i in range(500):
# 选择四张不同的图片(每个颜色一张)
images_to_copy = {color: image_lists[color].pop(0) for color in source_folders}
# 创建新的目标文件夹
target_folder = os.path.join(target_base_path, f'folder_{i+1}')
os.makedirs(target_folder)
# 复制图片到新文件夹
for color, image in images_to_copy.items():
source_path = os.path.join(source_folders[color], image)
target_path = os.path.join(target_folder, image)
shutil.copy2(source_path, target_path)
# 脚本执行完毕,所有图片已经归类完成
print("All images have been categorized into 500 folders with 4 different images each.") |