I have the following problem. I open png images with PIL Image.open()
. Is there a possiblity to read the xmp data after openening the image? I do not want to open the image twice, like i do it at the moment with Image.open(path)
and the libxmp library, in which the image is also opened to read the xmp data (xmp = file_to_dict(path)
).
Asked
Active
Viewed 2,193 times
0
-
Possible duplicate of [Read image XMP data in Python](https://stackoverflow.com/questions/6822693/read-image-xmp-data-in-python) – Alderven Feb 20 '19 at 12:05
-
@Alderven Unfornuatley not. It is for jpeg files:( – Varlor Feb 20 '19 at 12:29
-
Can you share PNG image with XMP data? – Alderven Feb 20 '19 at 12:48
1 Answers
1
If you use PIL's Image.open()
, it is in the text
attribute (and also in the info
attribute which contains the contents of the text attribute and some more stuff such as the resolution). ...which, in turn is a dict. In the images I looked at, it had only one entry, with key XML:com.adobe.xmp
, which holds the xmp data.
So you may want to do something like this:
from PIL import Image
import xml.etree.ElementTree as ET
im = Image.open(/path/tho/image.png) # replace with correct path
trees = [ET.fromstring(im.text[key]) for key in im.text.keys()]
And then you can inspect it, similar to how it is done, e.g., here:
for tree in trees:
nmspdict = {'x':'adobe:ns:meta/',
'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'dc': 'http://purl.org/dc/elements/1.1/'}
tags = tree.findall('rdf:RDF/rdf:Description/dc:subject/rdf:Bag/rdf:li',
namespaces = nmspdict)
tag_contents = [tag.text for tag in tags]
print(tag_contents)

0range
- 2,088
- 1
- 24
- 32