Click here to Skip to main content
Click here to Skip to main content

Face Collage

, 11 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Java code to create your own face collage


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.

Original Image


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.
// ######## 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.


  • 11th May, 2009: Initial version

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


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


About the Author

Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

QuestionThe java file does not run PinmemberMember 1036205426-Oct-13 8:52 
QuestionNo output PinmemberMember 855191011-Aug-12 22:55 
QuestionHELP SIR PLEASE i Really need your Help sir... please dont ignore PinmemberAnkur Gugar27-May-12 11:56 
Questionvery interesting PinmembertoSKan15-May-12 4:42 
GeneralCollage without repeating images Pinmemberckannan9023-Jun-10 1:57 
GeneralRe: Collage without repeating images Pinmemberabcbalaji23-Jun-10 2:36 
GeneralRe: Collage without repeating images Pinmemberckannan9023-Jun-10 2:53 
QuestionLess function? Just make a collage, ignoring the goal image? PinmemberMatt Jenner16-May-10 10:53 
AnswerRe: Less function? Just make a collage, ignoring the goal image? Pinmemberabcbalaji16-May-10 21:24 
GeneralMy vote of 1 Pinmemberankit8910210-May-10 0:34 
GeneralRe: My vote of 1 Pinmemberabcbalaji16-May-10 21:25 
GeneralRe: My vote of 1 Pinmemberabcbalaji16-May-10 21:26 
GeneralWonderful Pinmemberscolen21-Feb-10 15:13 
GeneralRe: Wonderful PinmemberGregory Truong22-Mar-10 13:40 
GeneralPlease Help. PinmemberGregory Truong14-Feb-10 16:57 
Question???? Pinmemberhopefresh16-Dec-09 20:05 
Generalhelp! Pinmemberhopefresh16-Dec-09 19:43 
GeneralRe: help! Pinmemberabcbalaji16-Dec-09 20:07 
Questionjava.lang.OutOfMemoryError PinmemberMember 19742641-Oct-09 8:27 
AnswerRe: java.lang.OutOfMemoryError Pinmemberabcbalaji1-Oct-09 22:17 
Questionplz ansr Pinmemberamandeep25s2-Sep-09 0:49 
Generalrespected sir Pinmemberamandeep25s1-Sep-09 6:24 
Generalwhere is the main method Pinmemberphateey8-Jun-09 6:47 
GeneralRe: where is the main method Pinmemberabcbalaji8-Jun-09 17:41 
GeneralPatented... PinmemberDrew Stainton11-May-09 13:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150301.1 | Last Updated 11 May 2009
Article Copyright 2009 by abcbalaji
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid