问题描述
我想将一组像素值从一个剖面颜色空间转换为另一个颜色空间,而不是这些值驻留在图像文件中,例如(比如)RGB / RGBA / CMYK / etc数据结构列表。 p>
我有Python和
最新的PIL非常支持 - 但没有办法交给它除了PIL映像(或遗留的pyCMS对象)之外的任何操作。
据我所知,LittleCMS包含的命令行工具 icctrans
会执行这种操作,但是我
lcms2.h
到 lcms2consts.py
与Python分发版中的 h2py.py
脚本。脚本不转换结构声明,但常量足以使用 ctypes
和 lcms2
作为基本颜色转换动态库。 此示例使用内置配置文件将双精度实验室中的单一颜色转换为8位sRGB。
使用 cmsOpenProfileFromFile(filename,'r')
import ctypes
来自ctypes import byref
来自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)
打印列表(outbuf)
lcms。 cmsDeleteTransform(xform)
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.
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)
这篇关于如何使用ICC配置文件对一组任意像素值(而不是图像数据结构)执行颜色变换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!