Skip to content

Instantly share code, notes, and snippets.

@schallis
Created September 9, 2011 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schallis/1206332 to your computer and use it in GitHub Desktop.
Save schallis/1206332 to your computer and use it in GitHub Desktop.
PIL compose thumbnail images
import Image
SPACING = 10
# Open source images
imgs = map(Image.open, ['1.gif', '2.gif', '3.gif', '4.gif'])
# Assume standard dimensions and store them
width, height = imgs[0].size
side_length = sqrt(len(imgs))
extra_space = (side_length - 1) * SPACING
# Create blank template image
background = Image.new('RGBA',
(width*2 + extra_space,
height*2 + extra_space),
(0,0,0,255))
# Compose images
background.paste(imgs[0], (0,0))
background.paste(imgs[1], (width+extra_space,0))
background.paste(imgs[2], (0,height+extra_space))
background.paste(imgs[3], (width+extra_space,height+extra_space))
# Save resulting image
background.save('out.gif')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment