Using PIL to read IPTC – python

One of the things that PIL can be used for is to read the IPTC information from an image. Here is a short example

from PIL import Image
from PIL import IptcImagePlugin

im = Image.open('2008-04-30_3.jpg')
iptc = IptcImagePlugin.getiptcinfo(im)
print iptc

Unfortunately the information is slightly cryptical

{
(2, 105): 'Perspektiv',
(1, 90): '\x1b%G',
(2, 116): '\xc2\xa9 2008 Jan Erik Mostro\xcc\x88m',
(2, 0): '\x00\x02',
(2, 25): 'ipernity',
(2, 120): 'Huset som sta\xcc\x8ar nere vid hamnen'
}

While it’s possible to read the values the keys doesn’t make much sense. In order to understand what they mean you need to look at the IPTC-NAA docs which tells you that those key values are actually some kind of labels. So to be able to read the various fields you need to do something like this

# Get headline
print iptc[(2,105)]

# Get caption
print iptc[(2,120)]

# Get copyright
print iptc[(2,116)]

# Get keywords
print iptc[(2,25)]

Which will give you the desired result

Perspektiv
Huset som står nere vid hamnen
© 2008 Jan Erik Moström
ipernity

Comments are closed.

blog comments powered by Disqus