Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 534
fish2000 Avatar asked Aug 30 '10 02:08

fish2000


People also ask

How do I change the color of my monitor profile?

To assign a color profile to a device, go to the Devices tab, and select your display device in the Device drop-down. Select the Use my settings for this device checkbox. This lets you make changes to that device's color profile settings.

How do I assign a color profile in Photoshop?

How To Change Color Profile In Photoshop. To change the color profile of your opened project in Photoshop, go to Image > Mode and choose your desired color profile. Here you can choose between RBG, CMYK, Grayscale, Lab Color, Index, and Bitmap profiles.

What happens when you assign a different color profile to an image?

Assigning a new color profile will almost always change the look of the image, but because the color values are the same, assigning the original color profile will return the original look of the image.


2 Answers

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)
like image 144
Janne Karila Avatar answered Sep 27 '22 17:09

Janne Karila


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.

like image 25
fish2000 Avatar answered Sep 27 '22 17:09

fish2000