Monday, July 2, 2012

Miniature Photo Album with ImageMagick (montage, convert) and Some Shell Scripting

How to develop photos smaller than 13x9, 15x11...

The fork is just there to indicate the size of the photos
When ordering photos online, you usually have the option to choose between formats like 13x9cm, 15x11cm, etc. However, none of these digital photo online services (that I know) offers smaller sizes. This post describes how to easily "merge" 4 JPG files into a big one (containing these 4 files in a 2x2-grid), which you can then order online and cut with scissors (or your other favorite cutting device) in order to create an album with photos of the size 7.5x5.5cm (or smaller, by adjusting some parameters)

Important: The shell script shown below does not touch the original files (it copies and rotates them). However, I don't give any warranty that you don't lose or ruin your original files, so make sure to have a backup!

Prerequisites

  • A set of JPEG images (optimally the same aspect ratio in order to get a seamless montage)
  • An installation of ImageMagick (the executables have to be in the $PATH)
  • A bash-terminal

How to do it

  • Move all the JPG files in a directory
  • cd into this directory and create an executable script with the content from below
  • Execute the script and wait
  • After that, the subdirectory generated contains the files you might want to upload to your online photo service  


#!/bin/bash

g=generated
t=$g/tmp

mkdir -p $t

### STEP 1 - cloning/rotating the file to 'portrait' 
### into the temporary subdirectory(0.jpg, 1.jpg, etc.)
i=0
IFS=$'\n'
for file in `find . -type f -iname "*.jp*g"`
do
 echo "rotating file $file into $t/$i.jpg"
 convert "$file" -rotate '90>' $t/$i.jpg

 let i=i+1
done


### STEP 2 - take 4 JPG files at a time 
### and compile them into new 2x2-images 
i=0
nroffiles=`ls $t/*.jpg | wc -w`
let nrofiterations=nroffiles/4
while [ $i -lt $nrofiterations ] 
do
 ## helper variables to access file names
 let f=i*4
 let a=f+0
 let b=f+1
 let c=f+2
 let d=f+3
 echo "creating a$i.jpg ($a.jpg $b.jpg $c.jpg $d.jpg)"

 ## compile the 4 separate images into a 2x2-grid. 
 ## please change the parameter 768x1024 if you 
 ## don't have a 3:4 aspect ratio
 montage $t/$a.jpg $t/$b.jpg $t/$c.jpg $t/$d.jpg -tile 2x2 -geometry 768x1024 $g/a$i.jpg
 let i=i+1
done

### remove the tmp directory
rm -rf $t



The images created by this script look somewhat like this one (individual pictures taken from http://www.freeimages.co.uk/)


Not very advanced stuff, I know, but I think it's a nice combination of some handy tools to make a nice present.

P.S. 1: I know that there are more elegant ways to write the shell-script, but for my purposes and skills this was the quickest way to go.

P.S. 2:  I think the 7.5x5.5cm format is optimal for a flip book. Wouldn't it be easy and fun to create your own flip book story with the continuous shooting mode of your digital camera?