Guest User

montage maker

a guest
Oct 24th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. import PIL
  2. import math
  3.  
  4. from PIL import Image
  5. import os
  6.  
  7. target_size = 4096
  8.  
  9. new_image = Image.new("RGBA", (target_size, target_size), None)
  10.  
  11. x = 1
  12. y = 1
  13.  
  14. def get_path():
  15.     path = "images/"
  16.     return path
  17.  
  18. images = []
  19.  
  20.  
  21. for path, dirs, files in os.walk(get_path()):
  22.      
  23.     for file_name in files:
  24.         images.append(path + file_name)
  25.  
  26. images.sort(key=os.path.getmtime, reverse=False)
  27. progress = 1
  28.  
  29. for image in images:
  30.  
  31.     side = round(math.sqrt(len(files))) + 2
  32.     cell_size = int((target_size / int(side))  * 1.0)
  33.    
  34.     im = Image.open(image, "r")
  35.  
  36.     bbox = im.getbbox()
  37.  
  38.     x_size = bbox[2]
  39.     y_size = bbox[3]
  40.  
  41.     multi = 1.0 / float(cell_size)
  42.  
  43.     scaler = float(y_size) * multi
  44.  
  45.     y_target = cell_size
  46.     x_target = int(x_size / scaler)
  47.  
  48.     im = im.convert("RGB")
  49.  
  50.     im2 = im.resize((x_target, y_target), Image.ANTIALIAS)
  51.     im3 =im.resize((640, 480), Image.ANTIALIAS)
  52.  
  53.     im3.save("export/progress_{}.png".format(progress), "PNG")
  54.     progress += 1
  55.    
  56.     if x + x_target < target_size:
  57.         new_image.paste(im2, (x, y))
  58.         x += x_target
  59.     else:
  60.         y += cell_size
  61.         x = 1
  62.         new_image.paste(im2, (x, y))
  63.         x += x_target
  64.  
  65. new_image.save("export/montage_output.png", "PNG")
  66. print ("finished")
  67.  
Advertisement
Add Comment
Please, Sign In to add comment