May 5, 2009

PHP: generate a photographic mosaic

I have written a command-line, procedural PHP script to generate a photographic mosaic.

Some samples:




The script works in two stages. The first one scans all of the images to work out an average color, and saves that information to a txt file on the disk.

This is then used in the second stage of the process, where a template of the mosaic is made - this is an image, shrinked down in size to as many pixels as is the number of images on the horizontal and vertical axis. This way each image represents one pixel of the template. The second stage begins composing the mosaic from images, that have the closest average color to the current pixel in the template.

The process is optimized by caching average colors to disk (this optimization is mandatory) and an caching smaller images to disk (there's a BIG difference in speed if you try to open a 5MB file or 100K file).

Here's the code that does all the work: http://pastebin.com/f70e3ccf7

3 comments:

Unknown said...

Very interesting!

Do u have a recently version of this? Any documentation?

Thanks!

Anonymous said...

Yeah I was wondering if you had the version with the improved color approximation as well.

jeancaffou said...

Best version is available on pastebin, link is in the article. Will posts samples later.