Created
February 24, 2011 21:38
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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