Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import os.path
- from glob import glob
- from PIL import Image
- WHITE = (255, 255, 255)
- def aspect_ratio(img):
- return int(img.size[0] * 10 / img.size[1]) * 0.1
- def in_range(img, size):
- x = abs(img.size[0] - size[0])
- y = abs(img.size[1] - size[1])
- return x <= 10 and y <= 10
- def jerk():
- images = {
- #1.0: {'size': (588, 588)},
- 1.5: {'size': (588, 388)},
- 2.0: {'size': (588, 288)},
- }
- for ratio, data in images.items():
- images[ratio]['base'] = Image.new('RGB', data['size'], WHITE)
- images[ratio]['files'] = []
- for i, fname in enumerate(glob('data/jerkcity*.gif')):
- if i and not i % 1000: print i
- img = Image.open(fname)
- ratio = aspect_ratio(img)
- size = images.get(ratio, {}).get('size', None)
- if size and in_range(img, size):
- images[ratio]['files'].append(fname)
- del img
- for ratio, data in images.items():
- count = len(data['files'])
- alpha = 1.0 / count
- size = data['size']
- base = data['base']
- result = base.copy()
- print '%s %s: Merging %s images' % (ratio, size, count)
- for fname in data['files']:
- img = Image.open(fname)
- if img.size != size:
- img = img.resize(size, Image.ANTIALIAS)
- img, tmp = base.copy(), img
- img.paste(tmp)
- del tmp
- result = Image.blend(result, img, alpha)
- result.save(os.path.join('tmp', '%s.png' % ratio))
- if __name__ == '__main__':
- jerk()
Add Comment
Please, Sign In to add comment