Polaroid Thumbnail Script

This is a bash script to create a thumbnail of a photograph that looks like a Polaroid photo with the caption written underneath. The thumbnail is randomly rotated slightly and has a drop shadow, e.g.

Example thumbnail

To use the script you must have a recent version of ImageMagick installed on your computer. You use the script by supplying three arguments:

  1. The input file name
  2. The output file name
  3. The caption (enclosed in quotes)

e.g. thumbnail.sh photo.jpg photo_sm.png "This is the caption"

The input and output file types can be any format, but I recommend using PNG as the output format, because the drop shadow is actually alpha-blended to work with any background and PNG preserves the alpha channel.

01 #!/bin/bash
03 # Copyright (c) Stephen C Phillips, 2006
05 in=$1
06 out=$2
07 label=$3
09 r=$((RANDOM%20-10))
11 #scale down to max 1000 first
12 convert -size 1000x1000 "$in" -resize 1000x1000 /tmp/photo.png
13 # make the label
14 width=`identify -format '%w' /tmp/photo.png`
15 convert -background white -fill black -gravity Center \
16 -pointsize 60 -font Helvetica -size ${width}x \
17 caption:"$label" /tmp/label.png
18 #add the label to photo
19 convert -append /tmp/photo.png /tmp/label.png \
20 /tmp/straight.png
21 #add border
22 mogrify -bordercolor white -border 20x20 /tmp/straight.png
23 #echo Rotating by $r
24 convert -background transparent -rotate $r /tmp/straight.png \
25 /tmp/rot.png
26 size=`identify -format '%w %h' /tmp/rot.png`
27 #echo Size after rotation is $size
28 x=`echo $size | cut -d' ' -f1`
29 y=`echo $size | cut -d' ' -f2`
30 x=$((x/5))
31 y=$((y/5))
32 #echo $x by $y
33 #this resize takes into account the size after the rotation
34 mogrify -resize ${x}x${y} -bordercolor transparent -border \
35 10x10 /tmp/rot.png
37 #add shadow
38 convert /tmp/rot.png -channel matte -negate -separate \
39 /tmp/mask1.png
40 convert /tmp/mask1.png -blur 0x5 +matte /tmp/mask2.png
41 convert /tmp/mask1.png -fill \#444 -draw 'color 0,0 reset' \
42 /tmp/gray.png
43 composite /tmp/mask2.png /tmp/gray.png -compose CopyOpacity \
44 /tmp/shadow.png
45 composite /tmp/rot.png /tmp/shadow.png "$out"

If you like, you can download the script. It is licensed under the GNU Public License.