首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:从png文件读取xmp数据

Python:从png文件读取xmp数据
EN

Stack Overflow用户
提问于 2019-02-20 20:00:10
回答 1查看 1.4K关注 0票数 0

我有以下问题。我用PIL Image.open()打开png图像。打开图像后是否可以读取xmp数据?我不想打开图像两次,就像我现在使用Image.open(path)和libxmp库一样,在libxmp库中,图像也被打开以读取xmp数据(xmp = file_to_dict(path))。

EN

回答 1

Stack Overflow用户

发布于 2019-02-20 21:54:34

如果你使用 PIL 的 Image.open(),那么它在text属性中(也在info属性中,该属性包含文本属性的内容和一些更多的内容,比如分辨率)。...which,反过来又是一个字典。在我查看的图像中,它只有一个条目,其关键字为XML:com.adobe.xmp,用于保存xmp数据。

因此,您可能想要这样做:

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54785908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档