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.

    2. I am new to GitHub and I have lots to say

      I DONT GIVE A FUCK ABOUT THE FUCKING CODE! i just want to download this stupid fucking application and use it

      WHY IS THERE CODE??? MAKE A FUCKING .EXE FILE AND GIVE IT TO ME. these dumbfucks think that everyone is a developer and understands code. well i am not and i don't understand it. I only know to download and install applications. SO WHY THE FUCK IS THERE CODE? make an EXE file and give it to me. STUPID FUCKING SMELLY NERDS

      ;) -

  6. 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.

  7. It would be great if the order was alphabetical, left to right, top down.

    1. That's an easy fix, just sort the images list before creating the collage. Under line 100 add images.sort()