Varlor
Varlor

Reputation: 1471

Python: Read xmp data from png files

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)).

Upvotes: 0

Views: 2655

Answers (1)

0range
0range

Reputation: 2158

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)

Upvotes: 1

Related Questions