我有以下问题。我用PIL Image.open()打开png图像。打开图像后是否可以读取xmp数据?我不想打开图像两次,就像我现在使用Image.open(path)和libxmp库一样,在libxmp库中,图像也被打开以读取xmp数据(xmp = file_to_dict(path))。
发布于 2019-02-20 21:54:34
如果你使用 PIL 的 Image.open(),那么它在text属性中(也在info属性中,该属性包含文本属性的内容和一些更多的内容,比如分辨率)。...which,反过来又是一个字典。在我查看的图像中,它只有一个条目,其关键字为XML:com.adobe.xmp,用于保存xmp数据。
因此,您可能想要这样做:
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()]然后,您可以检查它,类似于它是如何完成的,例如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)https://stackoverflow.com/questions/54785908
复制相似问题