Inspired by Jamie Zawinski's WebCollage perl program I've implemented something similar, though not as impressive functionally, in Java. An example of the output is here.
It's structured as a three stage processing pipeline hooked together using property listeners. The stages are:
- An image source. This chooses a number of images to create a collage from. It has an
imagesproperty that changes when it makes a new set of images available. - A collage creator. This waits for changes to the image source's
imagesproperty and converts the images into a collage. When the collage is ready the collage creator'scollageproperty changes. - One or more collage receivers. These take collages and display them or save them as files. They listen for changes to the
collageproperty of the collage creator.
Currently I've implemented two image sources. They both read images from a directory. One produces a single set of images, the other produces images every n seconds. I intend to write an image source that chooses random images from web pages, using Google as a search engine to select them.
There are three collage receivers. One displays the collage in a window, one saves the collage as a png file, overwriting the previous contents, and the last saves the image as a png file under a unique name in a directory.
The algorithm for producing the collage is currently fairly simple. It starts with a list containing an initial rectangular area the size of the collage to be produced. An image is randomly drawn somewhere in the rectangle with the clip area set so that it does not spill too far outside the rectangle and blending used to combine it with the background. The remaining area is then divided into rectangles by walking clockwise round the image. The original area is removed from the area list and the new rectangles added. Then the largest area in the list is chosen and the process repeated until the list is empty. I'm thinking about attempting to rotate some images in the future, as this might make the collages look less blocky, though it will make the calculation of the remaining areas more difficult.
As the code uses java.awt.Toolkit to load images it needs some sort of display to work. This presents a problem for headless unix systems. I'm going to test using Xvfb in this situation. An executable jar file is here, use java -jar collage.jar, and the source is here. If you use java -jar collage.jar --help you should get some help on the options.