Advertisement
Guest User

Python Composite Image

a guest
Apr 25th, 2016
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Composite generation script using PIL
  3.  
  4. import sys
  5. import os.path
  6. import PIL
  7. from PIL import Image
  8.  
  9.  
  10.  
  11. input_dir = r"C:/unfavorable semicircle/!LockKF/" #Path to directory of key frames
  12. x = 160         #Initial X
  13. y = 0           #Initial Y
  14. width = 197     #Width of the composite image
  15. height = int(50000/width)   #Approxomite height of the image given width
  16.                             #Change dividend based on how many images there are
  17.  
  18. files = os.scandir(input_dir)   #Gets all pictures in directory
  19. thumbnail_size = (1, 1)         #How big each image is in composite
  20. comp = Image.new("RGB", (width, height), "white") #Creates an image object for composite
  21.  
  22. # Loop through all files in dir
  23. for i in os.listdir(input_dir):
  24.     try:
  25.         # Attempt to open an image file
  26.         image = Image.open(input_dir + i)
  27.     except IOError:
  28.         # Report error, and then skip to the next argument
  29.         print("Problem opening" + " " + i)
  30.         continue
  31.  
  32.     # Resize the image
  33.     image = image.resize(thumbnail_size, Image.ANTIALIAS)
  34.    
  35.     # Add image to composite
  36.     comp.paste(image, (x,y))
  37.     print("pasted " + i)
  38.     x+= 1
  39.     if(x == width):
  40.         x = 0
  41.         y+= 1
  42.  
  43.    
  44. # Save the composite as Comp.png in the input directory.
  45. comp.save(input_dir + 'Comp.png')
  46. print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement