PIL/Pillow解码icc配置文件信息

PIL/Pillow decode icc profile information

我受困于 decoding/parsing 使用 PIL 提取的 ICC 配置文件信息。

下面是包含 "Adobe RGB (1998)" 配置文件的测试图像。

# download the test image:
wget http://i.stack.imgur.com/62AHB.jpg

-

from PIL import Image
path = '62AHB.jpg'
icc = Image.open(path).info.get('icc_profile') 

到目前为止一切顺利 - 但我找不到处理 returned ICC 信息的方法。

上面的例子将return:

'\x00\x00\x020ADBE\x02\x10\x00\x00mntrRGB XYZ\x07\xcf\x00\x06\x00\x03\x00\x00\x00\x00\x00\x00acspAPPL\x00\x00\x00\x00none\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\xf6\xd6\x00\x01\x00\x00\x00\x00\xd3-ADBE\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ncprt\x00\x00\x00\xfc\x00\x00\x002desc\x00\x00\x010\x00\x00\x00kwtpt\x00\x00\x01\x9c\x00\x00\x00\x14bkpt\x00\x00\x01\xb0\x00\x00\x00\x14rTRC\x00\x00\x01\xc4\x00\x00\x00\x0egTRC\x00\x00\x01\xd4\x00\x00\x00\x0ebTRC\x00\x00\x01\xe4\x00\x00\x00\x0erXYZ\x00\x00\x01\xf4\x00\x00\x00\x14gXYZ\x00\x00\x02\x08\x00\x00\x00\x14bXYZ\x00\x00\x02\x1c\x00\x00\x00\x14text\x00\x00\x00\x00Copyright 1999 Adobe Systems Incorporated\x00\x00\x00desc\x00\x00\x00\x00\x00\x00\x00\x11Adobe RGB (1998)\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00XYZ \x00\x00\x00\x00\x00\x00\xf3Q\x00\x01\x00\x00\x00\x01\x16\xccXYZ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00curv\x00\x00\x00\x00\x00\x00\x00\x01\x023\x00\x00curv\x00\x00\x00\x00\x00\x00\x00\x01\x023\x00\x00curv\x00\x00\x00\x00\x00\x00\x00\x01\x023\x00\x00XYZ \x00\x00\x00\x00\x00\x00\x9c\x18\x00\x00O\xa5\x00\x00\x04\xfcXYZ \x00\x00\x00\x00\x00\x004\x8d\x00\x00\xa0,\x00\x00\x0f\x95XYZ \x00\x00\x00\x00\x00\x00&1\x00\x00\x10/\x00\x00\xbe\x9c'

如何解码这些信息?

数据里面好像有一些。在这种情况下,我基本上只需要 "desc" 的值 "Adobe RGB (1998)"

有什么想法吗?期待您的意见 :) !

我不知道可以处理 ICC color profiles 的专用 Python 模块。

如果您喜欢冒险,请查看 ICC Profile Format Specification 的第 6 "Requirements" 部分。这应该让您开始了解如何解释字节。

Pillow 的 test folder contains two ICC profiles though, so perhaps it is worthwhile digging through the code of some of their tests. You might also want to look at this answer which refers to Little CMS 似乎有一些 ICC 配置文件支持。

我知道这个问题已经解决了,但我只是添加这个,因为它可能对那些正在寻找一种方法来提取 Python 中的 ICC 配置文件信息的人有用。

作为 jpylyzer 软件(我是主要开发人员)的一部分,我曾经写过一些 Python 代码来提取 ICC 配置文件的主要 header 字段(这完全独立于任何外部库)。请参阅下面的 validate_icc 函数:

https://github.com/openpreserve/jpylyzer/blob/master/jpylyzer/boxvalidator.py#L598

如果您关注 this link and then scroll down a bit, you'll see an overview of all the reported properties. Note that the code does not actually read the information inside the ICC tags, but you could extend it based on the ICC profile specifications.

我写这篇文章也是为了那些来这里搜索有关如何在 Python 中处理 ICC 颜色配置文件信息的人。

Pillow fork of the original PIL library for Python includes a ImageCms module. Unfortunately the constructor for a profile requires a filename or a file-like object, so we have to do it sideways via io.BytesIO

import io

from PIL import Image
from PIL import ImageCms

image = Image.open('/tmp/DQ-Tool_Print_13x18cm.jpg')
icc = image.info.get('icc_profile')
f = io.BytesIO(icc)
prf = ImageCms.ImageCmsProfile(f)

现在 prf 包含一个颜色配置文件实例。看看这里的文档:https://pillow.readthedocs.io/en/stable/reference/ImageCms.html#PIL.ImageCms.CmsProfile