Read image XMP data in Python

dolma33 picture dolma33 · Jul 25, 2011 · Viewed 13.1k times · Source

Can I use PIL, like in this example?

I only need to read the data, and I'm looking for the easiest simplest way to do it (I can't install pyexiv).

edit: I don't want to believe that the only way to do this is with some library (python-xmp-toolkit, pyexiv2, ...) that needs Exempi and Boost. There must be another option!

Answer

dirac3000 picture dirac3000 · Nov 14, 2011

Well, I was looking for something similar, then I came across the PHP equivalent question and I translated the anwer to Python:

f = 'example.jpg'
fd = open(f)
d= fd.read()
xmp_start = d.find('<x:xmpmeta')
xmp_end = d.find('</x:xmpmeta')
xmp_str = d[xmp_start:xmp_end+12]
print(xmp_str)

you can then convert xmp_str and parse it with an XML API.