Guest User

Untitled

a guest
Apr 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os.path
  3. from glob import glob
  4.  
  5. from PIL import Image
  6.  
  7. WHITE = (255, 255, 255)
  8.  
  9. def aspect_ratio(img):
  10. return int(img.size[0] * 10 / img.size[1]) * 0.1
  11.  
  12. def in_range(img, size):
  13. x = abs(img.size[0] - size[0])
  14. y = abs(img.size[1] - size[1])
  15. return x <= 10 and y <= 10
  16.  
  17. def jerk():
  18. images = {
  19. #1.0: {'size': (588, 588)},
  20. 1.5: {'size': (588, 388)},
  21. 2.0: {'size': (588, 288)},
  22. }
  23.  
  24. for ratio, data in images.items():
  25. images[ratio]['base'] = Image.new('RGB', data['size'], WHITE)
  26. images[ratio]['files'] = []
  27.  
  28. for i, fname in enumerate(glob('data/jerkcity*.gif')):
  29. if i and not i % 1000: print i
  30. img = Image.open(fname)
  31. ratio = aspect_ratio(img)
  32. size = images.get(ratio, {}).get('size', None)
  33. if size and in_range(img, size):
  34. images[ratio]['files'].append(fname)
  35. del img
  36.  
  37. for ratio, data in images.items():
  38. count = len(data['files'])
  39. alpha = 1.0 / count
  40. size = data['size']
  41. base = data['base']
  42. result = base.copy()
  43. print '%s %s: Merging %s images' % (ratio, size, count)
  44.  
  45. for fname in data['files']:
  46. img = Image.open(fname)
  47. if img.size != size:
  48. img = img.resize(size, Image.ANTIALIAS)
  49.  
  50. img, tmp = base.copy(), img
  51. img.paste(tmp)
  52. del tmp
  53.  
  54. result = Image.blend(result, img, alpha)
  55.  
  56. result.save(os.path.join('tmp', '%s.png' % ratio))
  57.  
  58. if __name__ == '__main__':
  59. jerk()
Add Comment
Please, Sign In to add comment