Click here to Skip to main content
15,880,651 members
Articles / Programming Languages / Java / Java SE
Article

Face Collage

Rate me:
Please Sign up or sign in to vote.
4.17/5 (5 votes)
11 May 2009CPOL3 min read 298.8K   2.1K   19   25
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)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe java file does not run Pin
Member 1036205426-Oct-13 7:52
Member 1036205426-Oct-13 7:52 
QuestionNo output Pin
Member 964871811-Aug-12 21:55
Member 964871811-Aug-12 21:55 
QuestionHELP SIR PLEASE i Really need your Help sir... please dont ignore Pin
Ankur Gugar27-May-12 10:56
Ankur Gugar27-May-12 10:56 
Questionvery interesting Pin
toSKan15-May-12 3:42
toSKan15-May-12 3:42 
GeneralCollage without repeating images Pin
ckannan9023-Jun-10 0:57
ckannan9023-Jun-10 0:57 
GeneralRe: Collage without repeating images Pin
abcbalaji23-Jun-10 1:36
abcbalaji23-Jun-10 1:36 
GeneralRe: Collage without repeating images Pin
ckannan9023-Jun-10 1:53
ckannan9023-Jun-10 1:53 
QuestionLess function? Just make a collage, ignoring the goal image? Pin
Matt Jenner16-May-10 9:53
Matt Jenner16-May-10 9:53 
AnswerRe: Less function? Just make a collage, ignoring the goal image? Pin
abcbalaji16-May-10 20:24
abcbalaji16-May-10 20:24 
GeneralMy vote of 1 Pin
ankit891029-May-10 23:34
ankit891029-May-10 23:34 
GeneralRe: My vote of 1 Pin
abcbalaji16-May-10 20:25
abcbalaji16-May-10 20:25 
GeneralRe: My vote of 1 Pin
abcbalaji16-May-10 20:26
abcbalaji16-May-10 20:26 
GeneralWonderful Pin
scottliu201121-Feb-10 14:13
scottliu201121-Feb-10 14:13 
GeneralRe: Wonderful Pin
Gregory Truong22-Mar-10 12:40
Gregory Truong22-Mar-10 12:40 
GeneralPlease Help. Pin
Gregory Truong14-Feb-10 15:57
Gregory Truong14-Feb-10 15:57 
Question???? Pin
hopefresh16-Dec-09 19:05
hopefresh16-Dec-09 19:05 
Generalhelp! Pin
hopefresh16-Dec-09 18:43
hopefresh16-Dec-09 18:43 
GeneralRe: help! Pin
abcbalaji16-Dec-09 19:07
abcbalaji16-Dec-09 19:07 
Questionjava.lang.OutOfMemoryError Pin
Member 19742641-Oct-09 7:27
Member 19742641-Oct-09 7:27 
AnswerRe: java.lang.OutOfMemoryError Pin
abcbalaji1-Oct-09 21:17
abcbalaji1-Oct-09 21:17 
Questionplz ansr Pin
amandeep25s1-Sep-09 23:49
amandeep25s1-Sep-09 23:49 
Generalrespected sir Pin
amandeep25s1-Sep-09 5:24
amandeep25s1-Sep-09 5:24 
Generalwhere is the main method Pin
phateey8-Jun-09 5:47
phateey8-Jun-09 5:47 
GeneralRe: where is the main method Pin
abcbalaji8-Jun-09 16:41
abcbalaji8-Jun-09 16:41 
GeneralPatented... Pin
Drew Stainton11-May-09 12:59
Drew Stainton11-May-09 12:59 

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

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