How to build a photo mosaic

September 29 , 2015

Tags: post-it, how-to


I have recently been asked to create a photo mosaic, where little photos are organized to represent another big photo.

Some people asked me how to do this so I share this step-by-step indications as a step by step cheat-sheet.

Choosing the tool

Of all the tools that I tried to achieve to compose the photo mosaic, Metapixel provided the best result by far. Most of the other tools either crashed or gave poor results.

Metapixel is a command line tool so knowing how to use the console would be a useful prerequisite.

Selecting the photos

First thing first if you want to create such a mosaic you need to select the main photo and the photos that are going to compose it.

You will get better results if you choose as many and diverse photos as possible. I needed around 2000 photos for my mosaic, but YMMV a lot according to the size of the thumbs and size of the resulting image.

The nice part is that you don't need to care if they are blury, too dark or exposed. It will not be noticed as these photos will be thumb-sized and used to best fit the final photo’s features. This will ease the selection of the input photos.

Note that the mosaic effect will also reduce the blur of the final image.

Choosing the size of the main photo

Once you have picked the main photo you will have to decide its final size.

You will need to adjust the size of your image with a photo editor. In Gimp for instance: you can use the scale image tool in the Image menu. You should set the x and y resolution to at least 300 dpi (pixels/in). Then in the bellow selection box for the unit of the height and width you can pick cm and set the size of the photo.

gimp scale image function

Choosing the size of the thumbs

Then you need to pick the size of each thumbs composing the mosaic. It is mainly a matter of taste: the smaller the thumbs the nicer the result but the least discernible those thumbs will be too. You should basically try with different sizes to get the result that better fits you.

Note that for projects of a 1mX0,5m photo mosaic, thumbs size was 100, 150 or 200 pixel. three prints was nice but to my mind the one with the biggest thumbs gave the best results.

Stiching it all together

Metapixel is probably already in your package manager, so you only need to apt-get install (or yum install) it. Otherwise, you will find the binarys linked on the github page.

It requires you to do a little preparation pass on the photos that will be used as thumbs: Create a temporary folder for Metapixel to store the prepared thumbs and a binary index of the photos. All the photos that you want to put in the mosaic will be stored in the ~/mozaique-photos/ folder. The temporary folder for the thumbs is ~/tmp/.

You can run

metapixel-prepare --recurse ~/mozaique-photos/ ~/tmp/

This will recursively process all the photos stored in the mozaique-photos/ folder and subfolder and then put those thumbs in the tmp folder. As long as you do not add any new photos to the mosaic you don't need to rerun that command.

Now Metapixel is ready to look in that photo library and build a mosaic.

metapixel --library ~/tmp --width=200 --height=200 --metapixel ~/goal-photo.jpg ~/output.png

The width and height option here refer to the mosaic thumbs size. You might not get a great result in this mode because Metapixel picks for each thumb the best match in the photo library.The same photo can thus be picked a lot.

There are 2 ways to improve this.

First you can set up the minimal distance between two instances of the same photo.

To do so, use the --distance option.

metapixel --library ~/tmp --distance=4 --width=200 --height=200 --metapixel ~/goal-photo.jpg ~/output.png

In this example, there will be a minimum of four thumbs between two instances of the same thumb.

Another way to improve this is to modify the search algorithm of Metapixel.

The --search=local option allows you to force Metapixel to only pick a photo once. The downside is that you will probably need a lot more photos to compose the mosaic or Metapixel might just fail.

metapixel --search=local --library ~/tmp --distance=4 --width=200 --height=200 --metapixel ~/goal-photo.jpg ~/output.png

Cheating for even better results :-)

If you are not completely satisfied with the result but you don't have any more photos that you can use in the mosaic, you could try the Metapixel --cheat option . This option allows Metapixel to put in overlay of the mosaic the goal-photo.jpg with a defined transparency. 0 is fully transparent and 100 is fully opaque.

metapixel --library ~/tmp --distance=4 --width=200 --height=200 --cheat=40 --metapixel ~/goal-photo.jpg ~/output.png

I have found that a value around 30 or 40 is not too noticeable and adds a lot of finish to the result.

Going further

Depending on the type of image you want to compose, you might want Metapixel to search in the photot library with a different set of criterea. The criterea used by Metapixel are the YIQ color space.

  • Y stands for luminance (meaning a black and white version of the photo)
  • I and Q for chrominance.

By default Metapixel will look for a match taking into account all those 3 channels. You can set the weight for each channel, the weight are between 0 and 1. Note that the default value for each channel weight is 1. The channels option are -y-weight, -i-weight, -q-weight.

So if you would like to look for a match based on the luminance channel, you would do as follows:

metapixel --library ~/tmp --y-weight=1 --i-weight=0 --q-weight=0 --metapixel ~/goal-photo.jpg ~/output.png

For more info, the repo of Metapixel with the documentation is on github

Thanks again to Mark Probst for developping that tool.

Have fun!