This Java code can generate a collage consisting of hundreds of images, that, when viewed wholly, would look like another image.
I was exploring Picasa 3's collage options. The options were limited just to Picture Pile, Mosaic, Grid collage, etc. I was looking for an option that would make a collage forming an image. Surprisingly, I could not find out a tool on the Internet. So, I wrote a small Java program that would create a photo of photos. People who want to create a collage using their photos, to form an image of their face or another photo, can use this program. A sample of images are given below to understand how it would look.
Collage - Zoom View shows hundreds of small images
Using the Code
For those who want to generate a few collages like this, a few tips:
- At the start of the program, you will find the CUSTOMIZATION PART. There are six parameters defined in this part. Modify these to best fit your requirements. More details regarding parameters are below.
- mainImagePath is the location where you can keep your model image.
- sourceFilesPath is the folder where you keep large number of images which would form the collage. More the number of source images you have, better is the clarity of collage.
- CollageFilePath is the folder where you want the collage to be placed.
enlargeSize - I used this parameter because if the collage is of the same resolution as the original photo, the resolution of small images will be very less and hence you would not recognize the small images even when zoomed. So,
enlargeSize is the number of times the collage is bigger than the model image. E.g.: If your model image is of 1600*1200 and you give
3, then collage will be of 4800*3600 size.
YpixelsPerBlock are the number of pixels per each small block of the collage. Could be like 16, 12 or 12, 9 etc. Better to keep in 4:3 ratio as most of the images are in the same ratio. Smaller this pixel size, better the clarity of the collage image but the resolution of small images decreases and vice versa.
- Compile the Java code using Java 1.4 or higher version and execute it.
- Place the model image, source images in correct folders and create a folder to place collage too.
private static String mainImagePath = "C:/Image/MainImage.jpg";
private static String sourceFilesPath = "C:/Image/Source/";
private static String CollageFilePath = "C:/Image/Collage/";
private static int enlargeSize = 4;
private static int XpixelsPerBlock = 12;
private static int YpixelsPerBlock = 9;
Points of Interest
For techies who want to know more about the code:
- Read the model image. For every small block (as per
YpixelsPerBlock), find the average RGB components.
- Read all the source images and find out the average RGB components for each image. (not each block here)
- For each block in the model image, compare its RGB with each source images RGB (I used root of sum of squares of differences for R G and B) and find out which source image's components have minimum deviation from the block's components. Store this match in the array.
- Now you construct the collage. Use each block's matching source image.
- 11th May, 2009: Initial version
Please leave a comment or mail me if you have suggestions.