Sunday, July 13, 2014

Picture collage maker using Python

Yesterday I decided to make a program for creation a collage from images. 
I've selected the next important input parameters: images list, output resulting collage image, width of resulting collage and the value of images' initial height (this value is approximate and will change, to fit the line of images in given width).
I've chosen the next algorithm:
1) Try to add images (resized to initial height) one by one to the first line:

And when the sum of their widths will go beyond the width value, start the next line.

2) After all images were added to the line, need to fit their size to width value. Here the height of images in the line will be changed from initial height.

3) Resize images to fit the width in all lines.

4) If one of the lines will contain only one image - need to repeat all steps from the beginning again, reducing the initial height value, to compact lines and to find a place for mentioned "alone" image.

I've also added shuffle of images, to get different collages each time.
The result of the program is the next:

Source code for collage maker is available on my github:
It's written in Python and uses PIL (Python Imaging Library).


  1. There is an error if the height of the image is less than the provided height(init_height) collages will have a black patch, to resolve this I have resized the image with height = init_height and proportional width.
    I have used this snippet:

    baseheight = init_height
    hpercent = (baseheight / float(img.size[1]))
    wsize = int((float(img.size[0]) * float(hpercent)))
    img = img.resize((wsize, baseheight), PIL.Image.ANTIALIAS)

    1. Exactly in which part of the code, did you make these changes?
      I am facing similar issues.
      Thank you.

  2. Great for porn sites, thank you

  3. Thanks for the work. I tried with example images and it works like said.
    It's also interesting for reading and learning code.
    Thanks again.

  4. Hi thanks for source code. I want to ask a question please. How can i make collage verticaly?

  5. Why is it so difficult? where is exe file and interface? What I am supposed to do with this code? I dont understand anything!

    1. it is not difficult if you take the time to learn a little python, a common programming language. There is no exe, because it is not a windows executable. The source code is at the link above, just before the comments section. It has the command line syntax.

  6. Sparkle Photo Effect & Pip Editor&Sparkle effect - Masterlogix best app to make your photo fully attractive and heart touching.
    You can use four tools in this app with many effects like photo frames, overlays effects, twinkle effects, photo cropping, photo filters and photo borders in this best Photo Editor App - Pip Camera - Sparkle Effect (Blur)

  7. Very interesting, good job and thanks for sharing such a good blog. Your article is so convincing that I never stop myself to say something about it. You're doing a great job. Keep it up.

  8. Blur background Photo Editor & Blur image Background Effect on image effective blur background app to give your picture DSLR blur effect. Select a photo from your mobile gallery.