Hi! This is my first blog post on this website, which is as of right now dedicated to my programming projects. I will be posting about the process of making them, and I’ll routinely update them on Github repositories on my account.
The project that I’m starting this blog off with is a program to take an image and make a photomosaic, like so:
That is much better than what I ended up getting as a minimum viable product, but we made progress.
I coded my version with Python (as most of my projects probably will be), and I heavily relied on Pillow and
However, loading my first image was really stressful because I kept getting an IOError saying that there was a “broken data stream when reading image file”, and even at the time of writing this I have no idea why it happened. When I made the scraper to get a dataset of images later on, every so often it would run into the same error for seemingly random images, and I ended up just writing a try statement to log and skip it.
After that, I defined a variable for how large I wanted the smaller images to be (50 x 50 pixels), trimmed the original image so that boxes of that size would evenly fit into the image, and iterated through the image with 50 x 50 subrectangles that were each loaded into a
To make a photomosaic, I obviously needed a set of images, so I wrote a separate script to collect some. Using the requests and
Finally, in the original script I iterated over each element in the second numpy array that held the average RGB values for the subrectangles, and found the image whose average RGB value (handily in my JSON index) was closest to that pixel’s average value, and saved the image’s filename in a third array. Heaton’s post suggests using “Pythagoras’ theorem in three dimensions”, which I used without the square root to make calculations faster. After saving the corresponding filenames, I iterated over that array to get each image and pasted the image in the corresponding place over the original image (technically a copy of the original image), generating my photomosaic.
Source code here.