Skip to content

Instantly share code, notes, and snippets.

@sansumbrella
Created February 24, 2011 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sansumbrella/842943 to your computer and use it in GitHub Desktop.
Save sansumbrella/842943 to your computer and use it in GitHub Desktop.
Walks directory for image files and merges them into a large sheet at a fixed individual size.
#!/usr/bin/env python
# encoding: utf-8
import Image
import sys
import os
def main():
path = "where-images-may-live"
columns = 10
column = 0
row = 0
width, height = 216, 120
extensions = ["png"]
images = getImages(path, extensions)
mosaic = Image.new( "RGB", ( columns*width, height *int( 1 + len(images)/columns ) ) )
for imgPath in images:
print "Adding image: " + imgPath
addImage( imgPath, width*column, row*height, width, height, mosaic )
column += 1
if( column == columns ):
column = 0
row += 1
print "Saving mosaic"
mosaic.save( path + '-mosaic.png' )
def getImages(path, extensions):
ret = []
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
if( extensions.count( name[-3:] ) != 0 ):
ret.append( os.path.join( root, name ) )
return ret
def addImage(path, x, y, width, height, mosaic):
img = Image.open(path)
imgOut = img.resize( (width, height), Image.BILINEAR )
cropBox = (0, 0, width, height)
pasteBox = ( x, y, x + width, y + height )
region = imgOut.crop( cropBox )
mosaic.paste( region, pasteBox )
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment