Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java / JavaSE

Face Collage

4.17/5 (5 votes)
11 May 2009CPOL3 min read 301.1K   2.1K  
Java code to create your own face collage

Introduction

This Java code can generate a collage consisting of hundreds of images, that, when viewed wholly, would look like another image.

Background

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.

Original Image

Collage

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:

  1. 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.
  2. mainImagePath is the location where you can keep your model image.
  3. 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.
  4. CollageFilePath is the folder where you want the collage to be placed.
  5. 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 enlargeSize as 3, then collage will be of 4800*3600 size.
  6. XpixelsPerBlock, 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.
  7. Compile the Java code using Java 1.4 or higher version and execute it.
  8. Place the model image, source images in correct folders and create a folder to place collage too.
Java
// ######## CUSTOMIZATION PART ######## - start

// Main image that is formed by collage of images. Must be a bitmap.
private static String mainImagePath = "C:/Image/MainImage.jpg";

// Folder that contains bitmaps which form the collage
private static String sourceFilesPath = "C:/Image/Source/";

// Location where result image to be created.
private static String CollageFilePath = "C:/Image/Collage/";

// The number of times, final collage image to be bigger than actual image, 
// so that the small images are clear when zoomed
private static int enlargeSize = 4;

// Number of pixels in each small block of the actual image. 
// Smaller the pixels, better the final collage. 
// Higher the pixels, small images in final collage look clear. 
// Better to keep the same x-y ratio(4:3 generally) as your source images.
private static int XpixelsPerBlock = 12;
private static int YpixelsPerBlock = 9;

// ######## CUSTOMIZATION PART ######## - end

Points of Interest

For techies who want to know more about the code:

  1. Read the model image. For every small block (as per XpixelsPerBlock, YpixelsPerBlock), find the average RGB components.
  2. Read all the source images and find out the average RGB components for each image. (not each block here)
  3. 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.
  4. Now you construct the collage. Use each block's matching source image.

History

  • 11th May, 2009: Initial version

Please leave a comment or mail me if you have suggestions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)