Skip to content

Instantly share code, notes, and snippets.

@neyer
Created May 19, 2014 17:31
Show Gist options
  • Save neyer/8fb651b804f30e259073 to your computer and use it in GitHub Desktop.
Save neyer/8fb651b804f30e259073 to your computer and use it in GitHub Desktop.
image mixer
from PIL import Image
from PIL import ImageDraw
import lazyopt
import random
import os
this_dir = os.getcwd()
images_dir = os.path.join(this_dir,'images')
image_count_limit = 100
target_size = (512,512)
def make_result_filename():
global images_dir, image_count_limit, target_size
return "%s-%s-%s-%s.jpg" % (images_dir, image_count_limit,
target_size[0], target_size[1])
def main():
lazyopt.apply_all()
print "get image paths from", images_dir
paths = []
for image_class in os.listdir(images_dir):
image_class_dir = os.path.join(images_dir, image_class)
for image in os.listdir(image_class_dir):
paths.append(os.path.join(image_class_dir, image))
# blend all of the images together, do these in pairs
if not paths:
print 'no images!'
return
num_images = len(paths)
if num_images > image_count_limit:
num_images = image_count_limit
random.shuffle(paths)
paths = paths[:num_images]
print 'opening %d images' % num_images
resized = [ Image.open(p).resize(target_size) for\
p in paths ]
print 'combining images'
while num_images > 1:
im1, im2 = resized.pop(), resized.pop()
blended = Image.blend(im1,im2,0.5)
resized = [blended] + resized
num_images -= 1
result = resized[0]
filename = make_result_filename()
print 'saving result to %s' % filename
result.save(filename)
result.show()
if __name__ == '__main__' : main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment