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
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.
A search through the PIL source (1.1.7) tells me that it can recognize XMP information in Tiff files, but I cannot find any evidence of a documented or undocumented API for working with XMP information using PIL at the application level.
From the CHANGES file included in the source:
+ Support for preserving ICC profiles (by Florian Böch via Tim Hatch).
Florian writes:
It's a beta, so still needs some testing, but should allow you to:
- retain embedded ICC profiles when saving from/to JPEG, PNG, TIFF.
Existing code doesn't need to be changed.
- access embedded profiles in JPEG, PNG, PSD, TIFF.
It also includes patches for TIFF to retain IPTC, Photoshop and XMP
metadata when saving as TIFF again, read/write TIFF resolution
information correctly, and to correct inverted CMYK JPEG files.
So the support for XMP is limited to TIFF, and only allows XMP information to be retained when a TIFF image is loaded, possibly changed, and saved. The application cannot access or create XMP data.
XMP metadata can be found in applist
.
from PIL import Image
with Image.open(filename) as im:
for segment, content in im.applist:
marker, body = content.split('\x00', 1)
if segment == 'APP1' and marker == 'http://ns.adobe.com/xap/1.0/':
# parse the XML string with any method you like
print body
reading raw file metadata does not work
This thread started 8 years ago and things might have evolved. I am not very deep in xmp and xml and I think I do not want to be. What I need to do though is to read and write metadata to image files (keywords ratings and the like).
So python-xmp-toolkit seems to be the best way to do that. It is the python layer based on Exempi as far as I understood.
So everything goes fine for jpg files. I get a dict with the keys:
http://ns.adobe.com/xap/1.0/mm/
http://ns.adobe.com/xap/1.0/
http://purl.org/dc/elements/1.1/
http://ns.adobe.com/camera-raw-settings/1.0/
http://ns.adobe.com/lightroom/1.0/
http://ns.adobe.com/tiff/1.0/
http://ns.adobe.com/exif/1.0/
with those I can access any metadata I wish.
But when I would like to do the same thing on a raw or sidecar file eg. .raf or .xmp the dict is empty. What am I doing wrong?
from libxmp.utils import file_to_dict
xmpPath = '/Users/me/image0001.jpg'
xmpDict = file_to_dict(xmpPath)
for key in xmpDict.keys():
print(key)
I am also interested to know if there is a 'proper' easy way to do this.
In the mean time, I've implemented reading XMP packets using pure Python in PyAVM. The relevant code is here. Maybe this would be useful to you?
with open( imgFileName, "rb") as fin:
img = fin.read()
imgAsString=str(img)
xmp_start = imgAsString.find('<x:xmpmeta')
xmp_end = imgAsString.find('</x:xmpmeta')
if xmp_start != xmp_end:
xmpString = imgAsString[xmp_start:xmp_end+12]
xmpAsXML = BeautifulSoup( xmpString )
print(xmpAsXML.prettify())
Or you can use the Python XMP Toolkit