# -*- coding: utf-8 -*- import os from PIL import Image current_dir_path = os.path.dirname(os.path.abspath(__file__)) # 设置起始目录 start_dir = os.path.join(current_dir_path, 'downloads') # 遍历downloads文件夹 for root, dirs, files in os.walk(start_dir): for dir in dirs: sub_dir = os.path.join(root, dir) for sub_root, sub_dirs, sub_files in os.walk(sub_dir): for sub_sub_dir in sub_dirs: sub_sub_dir_path = os.path.join(sub_root, sub_sub_dir) print(sub_sub_dir_path) png_count = 0 images = [] for file in os.listdir(sub_sub_dir_path): if file.lower().endswith('.png'): images.append(os.path.join(sub_sub_dir_path, file)) png_count += 1 if not images: raise ValueError("图片列表不能为空") total_image = Image.open(images[0]) for image in images[1:]: img = Image.open(image) new_image = Image.new('RGB', (max(total_image.width, img.width), total_image.height + img.height)) new_image.paste(total_image, (0, 0)) new_image.paste(img, (0, total_image.height)) total_image = new_image total_image.save(f'{sub_sub_dir_path}.png') break break break