Blog home

Tue, 07 Aug 2007

ImageMagick

ImageMagick is a software suite to create, edit, and compose bitmap images. Unlike more familiar pieces of software that manipulate bitmap images (Photoshop, Fireworks, etc.), ImageMagick does not have to use a graphical interface. Instead, it is usually invoked from a command line. This may seem to be a malformed concept: how could one edit an image without being able to see it? In fact, there are many operations that we might want to carry out on images that do not require us to view it while doing so

Where ImageMagick really comes into its own is when you have to carry out the same operation (or series of operations) on tens or hundreds of images. Because it can be invoked from the command line, it can easily be scripted using competent command line interpreters (such as Bash).

Gel buttons

Pill logo

This was a concept logo created for the online-only journal Archives of Drug Information.

Start with a two-tone oval shape

convert -size 200x120 xc:none \
    -fill red  -draw 'circle  50,60  20,60' \
    -draw 'rectangle  50,30  100,90' \
    -fill yellow  -draw 'circle  150,60  180,60' \
    -draw 'rectangle  100,30  150,90' \
    pill_shape.png

Add a higlight to the coloured shape using a modified blurred shade operation

convert pill_shape.png \
    \( +clone -fx A +matte  -blur 0x12  -shade 110x0 -normalize \
    -sigmoidal-contrast 16,60% -evaluate Multiply .5 \
    -roll +5+10 +clone -compose Screen -composite \) \
    -matte  -compose In  -composite  pill_highlight.png

Darken the borders to enhance the feel of a 3D object

convert pill_highlight.png \
    \( +clone -fx A  +matte -blur 0x2  -shade 0x90 -normalize \
    -blur 0x2  -negate -evaluate multiply .4 -negate -roll -.5-1 \
    +clone  -compose Multiply -composite \) \
    -matte  -compose In  -composite  pill_border.png

Add the text and a drop-shadow

convert pill_border.png \
    -font Tahoma  -pointsize 30  \
    -gravity center \
    -fill goldenrod -annotate 180x0+30+0 'ADI'  \
    -fill GhostWhite  -annotate 180x0+32+2 'ADI'  \
    -fill yellow -annotate 180x0+31+1 'ADI'  \
    -fill darkred  -annotate -32+0 'ADI'  \
    -fill AntiqueWhite3  -annotate -30+2 'ADI'  \
    -fill red    -annotate -31+1 'ADI'  \
    \( +clone -background navy -shadow 80x4+4+4 \) +swap \
    -background none  -flatten    pill_button.png

Finished pill button logo

Appending ownership notices

The following technique was used to update the images for the Roitt's Essential Immunology companion website. This site provides digital versions of all of the original images from the book 'Essential Immunology' in JPEG format. It was considered desirable to embed information about the origination of the images, so that if they were used in presentations (large versions are provided for this purpose) it would be clear from where they were obtained.

The label is 35 pixels tall, 24 point white text on grey background.

convert start_image.jpg -gravity Southwest \
    -background '#999999' -fill white -font Helvetica -pointsize 24 \
    -splice 0x35 -draw "text 3,3 'From:'" \
    -font Helvetica-Oblique -draw "text 70,3 'Roitt\'s Essential Immunology'" \
    -font Helvetica -draw "text 382,3 'Eleventh Edition'" \
    finish_image.jpg

For this to be a labour saving tip, we must avoid having to run this command individually for all 850-odd figures. To add a label to all images in a directory, we can make use of the capabilities of Bash.

for img in *.jpg
do
    convert $img -gravity Southwest \
        -background '#999999' -fill white -font Helvetica -pointsize 24 \
        -splice 0x35 -draw "text 3,3 'From:'" \
        -font Helvetica-Oblique -draw "text 70,3 'Roitt\'s Essential Immunology'" \
        -font Helvetica -draw "text 382,3 'Eleventh Edition'" \
        output/$img
done

The for img in *.jpg loops over all files with a .jpg extension in the current directory.

External links

permanent link