merge_images.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. import os
  3. from PIL import Image
  4. current_dir_path = os.path.dirname(os.path.abspath(__file__))
  5. # 设置起始目录
  6. start_dir = os.path.join(current_dir_path, 'downloads')
  7. # 遍历downloads文件夹
  8. for root, dirs, files in os.walk(start_dir):
  9. for dir in dirs:
  10. sub_dir = os.path.join(root, dir)
  11. for sub_root, sub_dirs, sub_files in os.walk(sub_dir):
  12. for sub_sub_dir in sub_dirs:
  13. sub_sub_dir_path = os.path.join(sub_root, sub_sub_dir)
  14. print(sub_sub_dir_path)
  15. png_count = 0
  16. images = []
  17. for file in os.listdir(sub_sub_dir_path):
  18. if file.lower().endswith('.png'):
  19. images.append(os.path.join(sub_sub_dir_path, file))
  20. png_count += 1
  21. if not images:
  22. raise ValueError("图片列表不能为空")
  23. total_image = Image.open(images[0])
  24. for image in images[1:]:
  25. img = Image.open(image)
  26. new_image = Image.new('RGB', (max(total_image.width, img.width), total_image.height + img.height))
  27. new_image.paste(total_image, (0, 0))
  28. new_image.paste(img, (0, total_image.height))
  29. total_image = new_image
  30. total_image.save(f'{sub_sub_dir_path}.png')
  31. break
  32. break
  33. break