简体   繁体   English

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

[英]How can one perform color transforms with ICC profiles on a set of arbitrary pixel values (not on an image data structure)?

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. 我想将一组像素值从一个配置文件颜色空间转换为另一个,而这些值不存在于图像文件中,例如(例如)RGB / RGBA / CMYK / etc数据结构列表。

I have Python and PIL at my disposal, but I'm interested in solutions in related environments if that's what it takes. 我可以使用Python和PIL ,但是如果需要的话,我对相关环境中的解决方案很感兴趣。

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. 最新的PIL对LittleCMS提供了非常好的支持 - 但是除了PIL图像(或传统的pyCMS对象)之外,没有任何办法可以处理它。

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. 据我icctransicctrans附带的命令行工具icctrans做了类似的事情,但我似乎无法在其上找到任何非骨架文档,文档将其称为演示工具。

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. 为了使用目前的2.3版本的小CMS与Python,我翻译lcms2.hlcms2consts.pyh2py.py附带在Python分发脚本。 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. 该脚本不会翻译结构声明,但常量足以使用ctypeslcms2作为动态库进行基本颜色转换。

This example transforms a single colour from double precision Lab to 8-bit sRGB using built-in profiles. 此示例使用内置配置文件将单一颜色从双精度Lab转换为8位sRGB。 Use cmsOpenProfileFromFile(filename, 'r') instead for files. 使用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)

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. 黑客方式:要重新配置N个颜色结构(和/或在颜色空间之间进行转换PIL.Image.new() ,使用yourimage.load()创建1x(N + 2)图像,使用yourimage.load()获取像素设置对象接口事物,并设置值(0,0)到(0,N)到你得到的任何东西。 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() . 将(0,N + 1)设置为白色,将(0,N + 2)设置为黑色,并使用您喜欢的ICC文件和PIL.ImageCms.ImageCmsTransform()转换(或校对 - 转换)该图像。 Blammo: that PIL object is now your LUT. Blammo:那个PIL对象现在是你的LUT。 Read the values off with the image.load() thingy and you're good. 使用image.load()读取关闭的值,你很好。

  • The true-nerd way: You need to use Python-colormath -- which is great for colorspace transforms but not profiling. 真正的书呆子方式:你需要使用Python-colormath - 这对于颜色空间变换很有用,但不能进行分析。 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. Colormath无法读取ICC配置文件,因此要么a)你要以可靠的方式解析他们疯狂的二进制格式,要么b) 只是按字面意思进行数学运算 This guy Bruce Lindbloom has all the data available in excel format for, like, all of the matricies you need to reprofile your LUTs. 这个人Bruce Lindbloom拥有excel格式的所有数据,例如,您重新构建LUT所需的所有基质。 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. 我仍然试图'只是'将这些数据提供给colormath,所以是的,这让我不那么棒了,因为我仍然试图让这种“书呆子”方式变成类似于生产质量的东西。

There you go. 你去吧 That is what I did so far to answer the question of freestanding, commando-style ICC LUT transforms. 这就是我到目前为止所做的回答独立,突击式ICC LUT变换的问题。 U guys, srsly. 呃,伙计们,srsly。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM