« Back to home

Diversions: A Corkboard Generation Script

Miserable wet weather and other commitments have prohibited me from getting out to the Lab to work on the AVR programmer and I don't have any code in a bloggable format yet so I had to come up with something else to write about.

The adaptor board

In a previous post I played around a bit with Photoshop to simulate the old Polaroid style instant film stock posted on a corkboard. You can see that image on the right.

I think it's a really nice way of showing a bunch of images in a single embedded image - a lot easier on the eye than trying to put all the individual images on one page and very well suited when you want to give an overview rather than focus on detail - for multiple shots of the same object for example, or a project in various stages of construction. Unfortunately it's a little bit fiddly to create, so I thought I'd try to automate the process.

Sample Image

The end result was a set of python scripts that would generate what I wanted from raw image files. The first is polaroid.py which scales, crops and optionally rotates a source image and places it in a frame that looks sort of like the old Polaroid Instamatic photos. It also adds an optional caption using a handwriting font.

Polaroid Version

You can see an example to the right. The script depends on the Python Imaging Library to do most of the heavy lifting and requires a file called caption.tff in the same directory as the script. The font I've placed there is a free for non-commercial use font I found on the web that is a nice readable handwriting font (unfortunately I've since lost the link to it, if you recognise the font please let me know so I can give credit where credit is due). You can put any TTF file there you like, just give it the same name and the script will use it instead.

If you run the script without parameters you will get the usage information as follows:

 Usage:

   polaroid.py [options] source-image alignment [caption]

 Where:

   source-image  name of the image file to transform. If no extension is                     specified .jpg is assumed.       alignment     one of 'top', 'left', 'bottom', 'right' or 'center'. This                     specifies the portion of the image to include in the final                     output. 'top' and 'left' are synonomous as are 'bottom' and                     'right'.       caption       If specified defines the caption to be displayed at the                     bottom of the image.

 Available options are:

   --clockwise     Rotate the image clockwise before processing       --anticlockwise Rotate the image anti-clockwise before processing

The alignment options are use to select which part of the image you want to appear in the final output. Because the Polaroid image is square I scale the source image so that the shortest side becomes 800px. It will then only use 800px in the other dimension for the image inserted in the output. You can choose whether this comes from the left, center or right of the image. If you rotate the image this becomes top, center or bottom. To simplify things top and left are treated the same as is bottom and right.

The next step is to lay a number of these images out on a corkboard background and spit out a single image. There is where the script corkboard.py comes in. This script simply takes a minimum of 3 and a maximum of 12 image file names on it's command line and randomly lays them out on a corkboard background. The images don't have to be generated by polaroid.py, they can be any image you like. The algorithm I use works best if all images are the same size, if the image sizes are different it will simply assume that all images are the size of the largest one it finds and offsets them accordingly.

A corkboard

You can see an example of the output to the left. Once again this script depends on the Python Imaging Library to do the image processing work and requires a single external resource, background.jpg, which must live in the same directory as the script. This background image is tiled across the entire background of the image before any of your images are placed down. It can be any size you like but for best results make sure it is a tileable image.

The script adds a drop shadow to each image, rotates them by a random amount and then places them randomly on the background. Because of the randomness involved you don't get the same output every time, if you don't like what has been generated, simply run it again. It always generates an output file called corkboard.jpg and it can be very large (depending on the size of your input images). You'll probably want to rename (and optionally scale) it before posting it up anywhere.

The end results aren't as pretty as the Photoshop version but you still get a very similar effect which is (IMHO) visually attractive. Even better, you can process a batch of images in a couple of minutes rather than spending an hour or two in Photoshop doing it.

It still wasn't quite automated enough for me so I wrote a shell script (it works on OS/X and Linux systems) that takes a single text specifying the images to process, the options to pass to polaroid.py and then invokes the scripts appropriately to generate the final corkboard. You can find that in my GIT Repository as well as the source for the python scripts and the supporting resources.

The scripts are straight forward and I've defined the control parameters at the top so it should be easy to make simple modifications to them. These are public domain so use and abuse as much as you like - a link back to my site for credit would be nice though.

If you come up with interesting modifications or enhancements you'd like to share please send them through to me and I'll update the repository accordingly.