【问题标题】:Read image XMP data in Python在 Python 中读取图像 XMP 数据
【发布时间】:2011-07-25 21:39:54
【问题描述】:

我可以像this example那样使用PIL吗?

我只需要读取数据,我正在寻找最简单的方法(我无法安装pyexiv)。

编辑:我不想相信这样做的唯一方法是使用一些需要 Exempi 和 Boost 的库(python-xmp-toolkitpyexiv2、...)。一定有别的选择!

【问题讨论】:

    标签: python image python-imaging-library xmp


    【解决方案1】:

    嗯,我一直在寻找类似的东西,然后我遇到了PHP equivalent 问题,我将答案翻译为 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)
    

    然后您可以转换 xmp_str 并使用 XML API 对其进行解析。

    【讨论】:

    • 我喜欢...在使用 PIL 之类的包访问数据时,总是遇到截断关键字的问题。另一个好处是,在编写可重用包时,从 jpg 读取它不会产生依赖关系。
    • 我必须用 'rb' 打开,然后 find(b'
    • XMP 现在可以在 jpeg 文件中分布在多个单独的部分中,这是该解决方案无法处理的情况。
    【解决方案2】:

    XMP 元数据可以在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
    

    【讨论】:

    • 很好,那个文件在哪里?我只找到github.com/python-pillow/Pillow/blob/…
    • 这并不总是有效,因为某些 jpeg 文件由于某种原因具有 APP1 标记“XMP\0://ns.adobe.com/xap/1.0/”,并且 \0 会中断split() 函数。
    • 如果正文中没有更多的空值,您可以改为使用content.rsplit(b'\x00', 1)b'http://ns.adobe.com/xap/1.0/' in marker
    【解决方案3】:

    我也很想知道是否有一种“适当”的简单方法可以做到这一点。

    同时,我已经在PyAVM 中实现了使用纯 Python 读取 XMP 数据包。相关代码为here。也许这对你有用?

    【讨论】:

      【解决方案4】:
      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())
      

      或者您可以使用 Python XMP 工具包

      【讨论】:

      • 当 XMP 分成多个部分时,由于 jpeg 格式仅允许此类数据的每个块为 64k,这将中断。
      【解决方案5】:

      对 PIL 源 (1.1.7) 的搜索告诉我,它可以识别 Tiff 文件中的 XMP 信息,但我找不到任何证据表明有文档或未记录的 API 用于在应用程序级别使用 PIL 处理 XMP 信息。

      来自源中包含的 CHANGES 文件:

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

      因此,对 XMP 的支持仅限于 TIFF,并且仅允许在加载、可能更改和保存 TIFF 图像时保留 XMP 信息。应用程序无法访问或创建 XMP 数据。

      【讨论】:

        猜你喜欢
        • 2019-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-26
        • 2016-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多