Batch Processing Images adding a Dropshadow with ImageMagick


Here’s the problem I was trying to solve. The books I write all use Apache FOP to render both plain HTML and PDF. Unlike some of the commercial (read “way too expensive”) FOP processors out there, Apache FOP doesn’t automatically scale/resample images for print resolution. The solution in DocBook is to include references to two images: a PNG at 72 DPI and a PDF at 150 DPI. For the past few years, I’ve just been managing this process with Photoshop macros and applying them in batch when needed. I had a series of macros that would add a dropshadow using PSD layers, and then automate the export to the appropriate formats.

Now while this sounds easy enough, think about a book that has 100 screenshots of a product that is constantly being updated. I replaced this manual process with the following bash script which uses ImageMagick to generate both web and print images with a drop-shadow. So, this solves a persistent problem that was causing me to waste too much time on production issues instead of focusing on content creation. I’m still having to deal with the fact that my DocBook contains two references to every image (two mediaobject elements for each figure). For my next step, I’m going to have to figure out how to dramatically simplify the docbook markup for authors: my goal is to have authors one worry about a single figure reference and to swap in the print-specific figure references right before I pass the XML to the XSLT that generates FO. (And if you don’t write books in DocBook this means nothing to you.)

I’m publishing it here just in case someone else finds it useful.

Here’s the Gist