linux, photography, WordPress

Reading iptc captions from jpegs with imagemagick

Rob and Spencer with zebras

Rob and Spencer with zebras

Once again I found myself needing to use imagemagick to do something, and was overwhelmed by the many options. After much fiddling around, I found out some options that worked for me.

In this case, I wanted to extract iptc captions from images, so that I could then insert the caption in a webpage with php. I use Picasa to edit photos and add captions. Picasa adds in the captions in the iptc information, which is the right place to add them. To extract the caption from the image above, do the following

convert robSpencer.jpg 8BIMTEXT:-

The result should be output to standard out:

2#120#Caption=”Rob and Spencer with zebras”

If you want to output to a file, simply do something like:

convert robSpencer.jpg 8BIMTEXT:filename.iptc

Now it easy to extract the caption using perl, python, php or whatever you like. For perl, we could simply pipe it:

convert robSpencer.jpg 8BIMTEXT:-|perl -ne '/Caption="(.*)"/; print $1;'

And in case you are interested, I needed to do this for the postie wordpress plugin which allows you to post to your blog via e-mail. In version 1.1.5 iptc captions will be read and displayed (if they are in the image you attach).