prepare_data.py 967 B

123456789101112131415161718192021222324252627282930313233343536
  1. from ipfml import processing
  2. from PIL import Image
  3. import shutil
  4. import os
  5. main_image_folder = "synthesis_images"
  6. images_folder = os.path.join(main_image_folder, "images")
  7. dest_folder = os.path.join(main_image_folder, "generated_blocks")
  8. if os.path.exists(dest_folder):
  9. # first remove folder if necessary
  10. os.rmdir(os.path.abspath(os.path.join(os.getcwd(), dest_folder)))
  11. # create new images
  12. images = os.listdir(images_folder)
  13. if not os.path.exists(dest_folder):
  14. os.makedirs(dest_folder)
  15. for img_path in images:
  16. img = Image.open(os.path.join(images_folder, img_path))
  17. blocks = processing.divide_in_blocks(img, (200, 200), pil=True)
  18. for id, pil_block in enumerate(blocks):
  19. img_name = img_path.split('/')[-1]
  20. split_name = img_name.split('.')
  21. block_name = split_name[0] + "_" + str(id) + "." + split_name[1]
  22. dest_img_block = os.path.join(dest_folder, block_name)
  23. pil_block.save(dest_img_block)