如何使用 ICC 配置文件对一组任意像素值(而不是图像数据结构)执行颜色转换?

发布于 2024-09-16 04:31:58 字数 460 浏览 6 评论 0 原文

我想将一组像素值从一个配置文件色彩空间转换为另一个,而不将这些值驻留在图像文件中,例如(例如)RGB/RGBA/CMYK/等数据结构的列表。

我有 Python 和 PIL 可供使用,但我对相关环境中的解决方案感兴趣如果这就是所需要的。

最新的 PIL 对 LittleCMS 提供了非常好的支持——但除了 PIL 图像之外没有办法提供任何东西(或旧版 pyCMS 对象)以供其执行操作。

据我所知,LittleCMS 中包含的命令行工具 icctrans 可以执行此类操作,但我似乎找不到任何关于它的非骨架文档,文档参考将其作为演示工具。

I'd like to convert a set of pixel values from one profiled colorspace to another, without these values residing in an image file, such as (say) a list of RGB/RGBA/CMYK/etc data structures.

I have Python and PIL at my disposal, but I'm interested in solutions in related environments if that's what it takes.

The latest PIL has very nice support for LittleCMS -- but no way to hand it anything other than a PIL image (or a legacy pyCMS object) for it to act upon.

As far as I can ascertain, the command-line tool icctrans that's included with LittleCMS does something of this sort, but I can't seem to find any non-skeletal documentation on it, and the documentation refers to it as a demonstration tool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩起发的微风 2024-09-23 04:31:58

为了在 Python 中使用当前 2.3 版本的 Little CMS,我翻译了 lcms2.hh2py.py 脚本将 code> 转换为 lcms2consts.py。该脚本不会转换结构声明,但常量足以使用 ctypeslcms2 作为动态库进行基本颜色转换。

此示例使用内置配置文件将单色从双精度 Lab 转换为 8 位 sRGB。使用 cmsOpenProfileFromFile(filename, 'r') 代替文件。

import ctypes
from ctypes import byref
from lcms2consts import *

lcms = ctypes.windll.lcms2

inprof = lcms.cmsCreateLab4Profile(0)
outprof = lcms.cmsCreate_sRGBProfile()
xform = lcms.cmsCreateTransform(inprof, TYPE_Lab_DBL, 
    outprof, TYPE_RGB_8,
    INTENT_PERCEPTUAL, 0)
lcms.cmsCloseProfile(inprof)
lcms.cmsCloseProfile(outprof)

DblTriplet = ctypes.c_double * 3
ByteTriplet = ctypes.c_ubyte * 3
inbuf = DblTriplet(60.1,20.2,0.5)
outbuf = ByteTriplet()
lcms.cmsDoTransform(xform, byref(inbuf), byref(outbuf), 1)
print list(outbuf)

lcms.cmsDeleteTransform(xform)

In order to use the current 2.3 version of Little CMS with Python, I translated lcms2.h to lcms2consts.py with the h2py.py script that comes in the Python distribution. The script does not translate struct declarations, but the constants are enough to do basic color transformations with ctypes and lcms2 as a dynamic library.

This example transforms a single colour from double precision Lab to 8-bit sRGB using built-in profiles. Use cmsOpenProfileFromFile(filename, 'r') instead for files.

import ctypes
from ctypes import byref
from lcms2consts import *

lcms = ctypes.windll.lcms2

inprof = lcms.cmsCreateLab4Profile(0)
outprof = lcms.cmsCreate_sRGBProfile()
xform = lcms.cmsCreateTransform(inprof, TYPE_Lab_DBL, 
    outprof, TYPE_RGB_8,
    INTENT_PERCEPTUAL, 0)
lcms.cmsCloseProfile(inprof)
lcms.cmsCloseProfile(outprof)

DblTriplet = ctypes.c_double * 3
ByteTriplet = ctypes.c_ubyte * 3
inbuf = DblTriplet(60.1,20.2,0.5)
outbuf = ByteTriplet()
lcms.cmsDoTransform(xform, byref(inbuf), byref(outbuf), 1)
print list(outbuf)

lcms.cmsDeleteTransform(xform)
夏の忆 2024-09-23 04:31:58

有两种方法。

  • 破解方法:要重新配置 N 个颜色结构(和/或在颜色空间之间转换它们),您可以使用 PIL.Image.new()< 创建一个 1x(N+2) 图像/code>,使用 yourimage.load() 获取像素设置对象接口事物,并将值 (0,0) 到 (0, N) 设置为您获得的值。将 (0, N+1) 设置为白色,将 (0, N+2) 设置为黑色,然后使用您最喜欢的 ICC 文件和 PIL.ImageCms.ImageCmsTransform() 转换(或校对转换)该图像。 >。 Blammo:那个 PIL 对象现在是你的 LUT。使用 image.load() 读取这些值就可以了。

  • 真正的书呆子方式:您需要使用Python -colormath——这对于色彩空间转换非常有用,但不适合分析。 Colormath 无法读取 ICC 配置文件,因此 a) 您可以以可靠的方式解析其疯狂的二进制格式,或者 b) 只是做数学计算,字面意思。 Bruce Lindbloom 拥有 Excel 格式的所有可用数据,例如 所有重新配置 LUT 所需的矩阵。他太棒了。我仍在尝试“只是”将这些数据输入到色彩数学中,所以是的,这让我不那么出色,因为我仍在尝试将这种“书呆子方式”转化为类似于生产质量的东西。

就这样吧。这就是我迄今为止为回答独立式、突击队式 ICC LUT 转换问题所做的工作。你们,srsly。

There are two ways.

  • The hack way: To reprofile N color structures (and/or transform them between colorspaces) you create a 1x(N+2) image with PIL.Image.new(), use yourimage.load() to get a pixel-setting object interface thing, and set values (0,0) through (0, N) to whatever you got. Set (0, N+1) to white and (0, N+2) to black, and transform (or proof-transform) that image using your favorite ICC files and PIL.ImageCms.ImageCmsTransform(). Blammo: that PIL object is now your LUT. Read the values off with the image.load() thingy and you're good.

  • The true-nerd way: You need to use Python-colormath -- which is great for colorspace transforms but not profiling. Colormath can't read ICC profiles, so either a) you get to parse their crazy binary format in a reliable way, or b) just do the math, literally. This guy Bruce Lindbloom has all the data available in excel format for, like, all of the matricies you need to reprofile your LUTs. He is totally awesome. I am still trying to 'just' feed this data into colormath, so yeah, that makes me less awesome as I am still trying to get this 'nerd way' into something resembling production-quality.

There you go. That is what I did so far to answer the question of freestanding, commando-style ICC LUT transforms. U guys, srsly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文