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:

8BIM#1028=”IPTC”
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).

Join 165 other subscribers

archives

  • 2024 (10)
  • 2023 (8)
  • 2022 (15)
  • 2021 (19)
  • 2020 (1)
  • 2019 (1)
  • 2018 (2)
  • 2017 (1)
  • 2016 (2)
  • 2015 (5)
  • 2014 (5)
  • 2013 (2)
  • 2011 (7)
  • 2010 (10)
  • 2009 (50)
  • 2008 (28)
  • 2007 (31)
  • 2006 (8)

Category