Creating Photo Mosaics Using Python | by Cody Glickman, PhD | Towards Data Science

Code updated December 8th 2021
Image_Tester.py no longer necessary
Thanks to Dan Black, Johannes la Pourte, and Jordan Maurice edits and comments
Photo mosaics are an image composed of many smaller images. The smaller images possess a similar color and brightness as a position in the original image making the original image visible from a distance similar to a Claude Monet painting. There exist many online tools (paid software) to create photo mosaics, however, I decided to implement a photo mosaic creator in python3 and share it with the community. My wife is creating a new lifestyle blog and I thought what better way to help her project than by creating a beautiful image from our wedding a little over a year ago. The source code is freely available on at the Data Dolittle GitHub. Let’s begin by setting up the environment necessary to run the scripts.
The photo mosaic program requires Pillow and numpy to process the images and matrix manipulation respectively. Follow the instructions from the text hyperlinks above to install both packages.
Do you have an image in mind that you would like to make a photo mosaic? Great! How about the images to comprise the smaller images? The more background images, the better the mosaic will appear. If you want to download a lot of images quickly I recommend using a chrome extension to download photos directly from google images. There exist other bulk image downloaders in Firefox. Place all the background images in the same folder. Keep your foreground image outside this folder. All your photos should be 3 channel images like jpeg or jpg. Be sure to run the Image_Tester.py script to check for 3 channel images in your background images folder. Remove any .png, .tiffs, or .gifs prior to creating a photo mosaic. The script will move images into a folder titled unusable images.
python Image_Tester.py --images [Background_Images_Folder]
Now that you have installed the necessary tools and have the photos, you are ready to create your photo mosaic. The required inputs include the target foreground image, the background images folder, and the size of the grid with two integers separated by a space. An optional parameter is the output flag to save the mosaic image as any name your desire.
python Mosaic_Creator.py --target [Input_Image] --images [Background_Images_Folder] --grid 50 50 --output [Output_Name]
This section contains the details of the creation engine so for those bored by pseudo-code scroll to the bottom to see the final product of my demo. The full code is available on GitHub.
The Image Directory: The background images are loaded into memory, reshaped, and processed. The processing involves acquiring the average red, blue, and green channels in the image. This average is what is used to place the background images into the grid.
The Target Image: The target image is broken into a grid defined by the user input. The cell dimensions are a product of the original image size and the number of splits due to the grid size. The average red, green, and blue values of each cell is calculated. The best match between the cell average and the background image average is used to place a background image into the cell. All cells are filled with background images and the resulting mosaic is saved to a file.
After running the script using our wedding photos, the following mosaic was created.
The mosaic looks like the original, only darker. Our wedding occurred at sunset, so many of the photos have dark backgrounds leading to a darkening of the mosaic image. Increasing the number of photos will provide more detail and having brightened photos will increase the overall brightness. This article detailed the tools and process to create a photo mosaic in python using the scripts available on GitHub. You are able to create your own photo mosaics using anything! My name is Cody Glickman and I can be found on LinkedIn. Be sure to check out some other articles below.