Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create a collage of images, filling all space without cropping. Implements algorithm presented in
#!/usr/bin/env python
from operator import itemgetter
from optparse import OptionParser
import logging
from PIL import Image
from math import ceil, floor
import sys
Copyright 2014 Trailbehind inc
Implements algorithm presented in
Linear partition algorithm from
def linear_partition(seq, k):
if k <= 0:
return []
n = len(seq) - 1
if k > n:
return map(lambda x: [x], seq)
values = [i[0] for i in seq]
table, solution = linear_partition_table(values, k)
k, ans = k-2, []
while k >= 0:
ans = [[seq[i] for i in xrange(solution[n-1][k]+1, n+1)]] + ans
n, k = solution[n-1][k], k-1
ans = [[seq[i] for i in xrange(0, n+1)]] + ans
return ans
def linear_partition_table(seq, k):
n = len(seq)
table = [[0] * k for x in xrange(n)]
solution = [[0] * (k-1) for x in xrange(n-1)]
for i in xrange(n):
table[i][0] = seq[i] + (table[i-1][0] if i else 0)
for j in xrange(k):
table[0][j] = seq[0]
for i in xrange(1, n):
for j in xrange(1, k):
table[i][j], solution[i-1][j-1] = min(
((max(table[x][j-1], table[i][0]-table[x][0]), x) for x in xrange(i)),
return (table, solution)
def create_collage(image_files, result_width, rows):
Create a collage of images, with <rows> rows, filling all space without cropping
logging.debug("Creating collage of %d images with %d rows" % (len(image_files), rows))
if len(image_files) < rows:
rows = len(image_files)
images = []
for filename_or_image in image_files:
if isinstance(filename_or_image, Image.Image):
im = filename_or_image
im =
aspect = float(im.size[0])/float(im.size[1])
images.append((aspect, im))
partioned_images = linear_partition(images, rows)
result_height = 0
for row in partioned_images:
row_width = float(sum([i[0] for i in row]))
aspect, image = row[0]
width_percent = aspect/row_width
image_width = width_percent * result_width
scale_ratio = image_width/float(image.size[0])
result_height += image.size[1] * scale_ratio
result_size = (result_width, int(result_height))
logging.debug("Creating result image size: %f*%f" % result_size)
result ='RGB', result_size)
y_position = 0
for row in partioned_images:
row_width = float(sum([i[0] for i in row])) #Sum of aspect ratios for all images in row
x_position = 0
row_height = 0
for aspect, image in row:
width_percent = aspect/row_width
image_width = width_percent * result_width
scale_ratio = image_width/float(image.size[0])
logging.debug(("Image size: %f %f" % image.size) + " percent of row " + str(width_percent) + " new width " + str(image_width) + " scale_ratio " + str(scale_ratio))
new_size = [int(ceil(i * scale_ratio)) for i in image.size]
logging.debug("resizing image to " + str(new_size))
resized_image = image.resize(new_size)
paste_position = (x_position, y_position)
logging.debug("pasting image at " + str(paste_position))
result.paste(resized_image, paste_position)
x_position += new_size[0]
row_height = new_size[1]
y_position += row_height
return result
def _main():
usage = "usage: %prog img.jpg img2.jpg ..."
parser = OptionParser(usage=usage,
description="Create a collage of images, filling all space, without cropping any images")
parser.add_option("-d", "--debug", action="store_true", dest="debug",
help="Turn on debug logging")
parser.add_option("-o", "--output", action="store", dest="output",
default=None, help="output to file, if no file is specified image will be displayed")
parser.add_option("-w", "--width", action="store", dest="width", type="int",
default=1000, help="Image width")
parser.add_option("-r", "--rows", action="store", dest="rows", type="int",
default=None, help="Number of rows, default is <number of images>/2")
(options, args) = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if options.debug else logging.ERROR)
if len(args) < 2:
logging.error("Error, must specify at least 2 images")
if options.rows is not None:
rows = options.rows
rows = len(args)/2
result = create_collage(args, options.width, rows)
if options.output:
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.