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 how the human eye works.
There are two kinds of mosaic, depending on how the matching is done. In 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 find a matching image from the images in the input folder specified 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 firstname.lastname@example.org. 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
- Implementing slicing in __getitem__
- ML | Implementing L1 and L2 regularization using Sklearn
- Implementing web scraping using lxml in Python
- Implementing Deep Q-Learning using Tensorflow
- Implementing Apriori algorithm in Python
- Implementing LRU Cache Decorator in Python
- Implementing Agglomerative Clustering using Sklearn
- Implementing Web Scraping in Python with BeautifulSoup
- Implementing DBSCAN algorithm using Sklearn
- Implementing Web Scraping in Python with Scrapy
- ML | OPTICS Clustering Implementing using Sklearn
- Python | Implementing Dynamic programming using Dictionary
- Python | Implementing 3D Vectors using dunder methods
- Implementing Shamir's Secret Sharing Scheme in Python
- Implementing Artificial Neural Network training process in Python