Advertisement
Guest User

Untitled

a guest
Dec 18th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. #! /usr/bin/python
  2. from PIL import Image
  3. from random import randint
  4.  
  5. # Sorts img kinda randomly
  6. source = Image.open("test.jpg")
  7. threshold = 150
  8.  
  9. img = source.load()
  10.  
  11. blackandwhite = source.convert("L").load()
  12.  
  13. canvas = Image.new("RGB", source.size)
  14.  
  15. newimg = canvas.load()
  16. count = source.size[0]
  17.  
  18. print (source.format)
  19. print (source.size)
  20. print (source.mode)
  21. print ("Threshold: ", threshold)
  22. print ("============================================")
  23.  
  24. counter = 0 #counter
  25. # do the loop twice because we want to make em fall!
  26. counter = 0
  27. for i in range(0, source.size[0]-1): # loop through every x value
  28.     vert_list = [] #list of this column
  29.     for pix in range(0, source.size[1]-1): #make a list of the column from the b&w img
  30.         #store greyscale-color pixel pairs
  31.         grey = blackandwhite[i, pix] #for being in color ^^
  32.         color = img[i, pix]
  33.         vert_list.append((grey, color))
  34.  
  35.     counter += 1
  36.     if counter % 10 == 0:
  37.         print(counter, "/", count)
  38.     #now remove all pixels brighter than the threshold
  39.     #each x is a greyscale-color pair, so x[0] is the greyscale value
  40.     vert_list[:] = (x for x in vert_list if threshold > x[0])
  41.  
  42.     top_spacing = source.size[1] - len(vert_list) #height
  43.     for pixel in range(0, len(vert_list)):
  44.         #each vert_list[pixel] is a greyscale-color pair, so its [1] is the color
  45.         newimg[i,pixel + top_spacing] = vert_list[pixel][1]
  46.  
  47.  
  48. canvas.save("fall.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement