A photomosaic is an image split into a grid of rectangles, with each replaced by another image that matches the target (the image you ultimately want to appear in the photomosaic). In other words, if you look at a photomosaic from a distance, you see the target image; but if you come closer, you will see that the image actually consists of many smaller images. This works because of the how the human eye works.
There are two kinds of mosaic, depending on how the matching is done. In the simpler kind, each part of the target image is averaged down to a single color. Each of the library images is also reduced to a single color. Each part of the target image is then replaced with one from the library where these colors are as similar as possible. In effect, the target image is reduced in resolution (by downsampling), and then each of the resulting pixels is replaced with an image whose average color matches that pixel.
In the more advanced kind of photographic mosaic, the target image is not downsampled, and the matching is done by comparing each pixel in the rectangle to the corresponding pixel from each library image. The rectangle in the target is then replaced with the library image that minimizes the total difference. This requires much more computation than the simple kind, but the results can be much better since the pixel-by-pixel matching can preserve the resolution of the target image.
How to create Photomosaics?
- Read the tile images, which will replace the tiles in the original image.
- Read the target image and split it into an M×N grid of tiles.
- For each tile, fnd the best match from the input images.
- Create the fnal mosaic by arranging the selected input images in an M×N grid.
Splitting the images into tiles
Now let’s look at how to calculate the coordinates for a single tile from this grid. The tile with index (i, j) has a top-left corner coordinate of (i*w, i*j) and a bottom-right corner coordinate of ((i+1)*w, (j+1)*h), where w and h stand for the width and height of a tile, respectively. These can be used with the PIL to crop and create a tile from this image.
Averaging Color Values
Every pixel in an image has a color that can be represented by its red, green, and blue values. In this case, you are using 8-bit images, so each of these components has an 8-bit value in the range [0, 255]. Given an image with a total of N pixels, the average RGB is calculated as follows:
For each tile in the target image, you need to fnd a matching image from the images in the input folder specifed by the user. To determine whether two images match, use the average RGB values. The closest match is the image with the closest average RGB value.
The simplest way to do this is to calculate the distance between the RGB values in a pixel to fnd the best match among the input images. You can use the following distance calculation for 3D points from geometry:
Now lets try to code this out
python test.py --target-image test-data/a.jpg --input-folder test-data/set1/ --grid-size 128 128
This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Implementing a Superellipse
- ML | Implementing L1 and L2 regularization using Sklearn
- Implementing Apriori algorithm in Python
- Implementing DBSCAN algorithm using Sklearn
- Implementing Agglomerative Clustering using Sklearn
- Implementing Web Scraping in Python with BeautifulSoup
- Implementing Web Scraping in Python with Scrapy
- Implementing web scraping using lxml in Python
- Python | Implementing 3D Vectors using dunder methods
- Implementing Artificial Neural Network training process in Python
- ML | One Hot Encoding of datasets in Python
- ML | Rainfall prediction using Linear regression
- Python | Generate test datasets for Machine learning
- Python | Making program run faster
Improved By : Akanksha_Rai