Advertisement
Guest User

Untitled

a guest
Jan 30th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. #!python
  2.  
  3. # Usage example:
  4. # python chas-conceptrate.py nicegirl_dataset results 42
  5.  
  6. from PIL import Image
  7. from random import randint
  8. import sys
  9. import os
  10.  
  11. source_dir = sys.argv[1]
  12. print("Processing directory: " + source_dir)
  13.  
  14. target_dir = sys.argv[2]
  15. print("Putting to directory: " + target_dir)
  16.  
  17. repeats = sys.argv[3]
  18. print("Every image will be repeated " + repeats + " time(s)")
  19. repeats = int(repeats)
  20.  
  21. try:
  22.     os.mkdir(target_dir)
  23. except:
  24.     print("Could not create directory: " + target_dir)
  25.     print("It may exist already, and then that's OK. The files in it will not be deleted but may be overwritten.")
  26.     print("Proceeding anyway...")
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. def process_image(filename, number_of_copies, allow_flip = True):
  36.     print("Processing file: " + filename + "  ...")
  37.     image = Image.open(source_dir + "/" + filename)
  38.     width, height = image.size
  39.     print("Width:", width)
  40.     print("Height:", height)
  41.  
  42.     for i in range(0, number_of_copies):
  43.         left_shift = randint(0,  width - 512)
  44.         top_shift  = randint(0, height - 512)
  45.  
  46.         cropped_image = image.crop( (left_shift, top_shift, left_shift + 512, top_shift + 512) )
  47.  
  48.         if randint(0,1) and allow_flip:
  49.             cropped_image = cropped_image.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
  50.             is_flipped = "flip"
  51.         else:
  52.             is_flipped = "noflip"
  53.  
  54.         cropped_image.save(
  55.             target_dir + "/" +
  56.             filename + "___" +
  57.             str(i) + "_" +
  58.             str(left_shift) + "_" +
  59.             str(top_shift) + "_" +
  60.             is_flipped + "_cropped.png"
  61.         )
  62.  
  63.  
  64. images = []
  65.  
  66. for path in os.listdir(source_dir):
  67.     # check if current path is a file
  68.     if os.path.isfile(os.path.join(source_dir, path)):
  69.         process_image(path,repeats)
  70.  
  71.  
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement