What is the preferred way of doing the conversion using PIL/Numpy/SciPy today?
-
I've read this question http://stackoverflow.com/questions/3228361/using-pil-and-numpy-to-convert-an-image-to-lab-array-modify-the-values-and-then and it gives a broken link to a color.py which is missing from scipy trunk. – Antony Hatchkins Nov 15 '12 at 20:50
-
Yes, I've found this file deep in the scipy git repository, but I can't beleive there's no standard way of doing such a simple thing using such powerful tools. – Antony Hatchkins Nov 15 '12 at 20:58
-
And yes, I know about code.google.com/p/python-colormath lib, and yet I can't understand why didn't it make its way into any of those three tools. – Antony Hatchkins Nov 15 '12 at 20:58
5 Answers
Since 2010 when the linked question was asked the corresponding code moved from scipy to a separate toolkit: http://scikit-image.org/
So here's the code I was actually looking for:
from skimage import io, color
rgb = io.imread(filename)
lab = color.rgb2lab(rgb)
It should also be noted that due to Lab nature srgb->lab conversion depends on an additional parameter: whitepoint, eg:
• Photoshop uses a white point called D50 (which is a standard for icc)
• OpenCV and skimage use D65 (which is a standard for srgb).
• default Matlab implementation uses D50 (it is capable of using others),
This nice FAQ explains it this way:
You should use D65 unless you have a good reason to use something else.
The print industry commonly uses D50 and photography commonly uses D55.
These represent compromises between the conditions of indoor (tungsten) and daylight viewing.
You can tell which whitepoint you're dealing with by converting RGB (0,0,255)
to Lab:
• D50 would give you (30, 68, -112)
• D55 (30, 73, -110)
• D65 (32, 79, -108)
The numbers after 'D' correspond to (internally) used color temperature of white point: D50 = 5003 K (yellowish), D65 = 6504 K (blueish)
I'm grateful to Alex and Roman for their answers because they pointed me into the right direction.

- 31,947
- 10
- 111
- 111
-
2In `skimage` you can change white point to D50 or something else http://stackoverflow.com/a/22968744/2863099 – Pylyp Apr 10 '14 at 07:00
-
I've found this code on the old Adobe Cookbook site and have adapted for Python. It doesn't require any third-party modules or components:
def rgb2lab ( inputColor ) :
num = 0
RGB = [0, 0, 0]
for value in inputColor :
value = float(value) / 255
if value > 0.04045 :
value = ( ( value + 0.055 ) / 1.055 ) ** 2.4
else :
value = value / 12.92
RGB[num] = value * 100
num = num + 1
XYZ = [0, 0, 0,]
X = RGB [0] * 0.4124 + RGB [1] * 0.3576 + RGB [2] * 0.1805
Y = RGB [0] * 0.2126 + RGB [1] * 0.7152 + RGB [2] * 0.0722
Z = RGB [0] * 0.0193 + RGB [1] * 0.1192 + RGB [2] * 0.9505
XYZ[ 0 ] = round( X, 4 )
XYZ[ 1 ] = round( Y, 4 )
XYZ[ 2 ] = round( Z, 4 )
XYZ[ 0 ] = float( XYZ[ 0 ] ) / 95.047 # ref_X = 95.047 Observer= 2°, Illuminant= D65
XYZ[ 1 ] = float( XYZ[ 1 ] ) / 100.0 # ref_Y = 100.000
XYZ[ 2 ] = float( XYZ[ 2 ] ) / 108.883 # ref_Z = 108.883
num = 0
for value in XYZ :
if value > 0.008856 :
value = value ** ( 0.3333333333333333 )
else :
value = ( 7.787 * value ) + ( 16 / 116 )
XYZ[num] = value
num = num + 1
Lab = [0, 0, 0]
L = ( 116 * XYZ[ 1 ] ) - 16
a = 500 * ( XYZ[ 0 ] - XYZ[ 1 ] )
b = 200 * ( XYZ[ 1 ] - XYZ[ 2 ] )
Lab [ 0 ] = round( L, 4 )
Lab [ 1 ] = round( a, 4 )
Lab [ 2 ] = round( b, 4 )
return Lab

- 3,103
- 6
- 36
- 61

- 2,209
- 5
- 23
- 44
-
It doesn't directly answer the question: I needed a one-liner. But it is helpful anyway. Thanks! – Antony Hatchkins Apr 17 '13 at 18:21
-
1Although I would reference the original [easyrgb](http://www.easyrgb.com/index.php?X=MATH) site instead of the adobe cookbook. – Antony Hatchkins Apr 17 '13 at 18:22
-
Your code is not quite pythonic should I say. At the very least I'd use `enumerate` instead of the `num` variable and `1/3.` instead of `0.3333333333333333` – Antony Hatchkins Apr 17 '13 at 18:32
-
This code appears to do gamma correction ( `** 2.4` ). What if I know my gamma is 2.2, not 2.4? Is it enough to change the exponent to 2.2, or are changes needed to other hard-coded constants (like the 1.055) ? – jez Jul 13 '15 at 15:10
-
2@jez the gamma 2.4 you see here is the sRGB standard. It joins together with a linear conversion at the lower values, and together they closely match a 2.2 gamma curve. I wouldn't change anything. – Mark Ransom Jul 07 '16 at 15:41
-
Thank you for working out this, I've tried a few values and they match those on http://colormine.org/convert/rgb-to-lab – imrek Apr 13 '17 at 07:05
Edit: Sample pyCMS code:
from PIL import Image
import pyCMS
im = Image.open(...)
im2 = pyCMS.profileToProfile(im, pyCMS.createProfile("sRGB"), pyCMS.createProfile("LAB"))
Edit: Pillow, the PIL fork, seems to have pyCMS built in.
You might use pyCMS (http://www.cazabon.com/pyCMS/) which works with PIL images.
If speed is not a factor, use python-colormath (http://code.google.com/p/python-colormath/).

- 19,689
- 9
- 86
- 158
-
`pyCMS` deals with ICC profiles, color spaces are "side-effect". I asked for a one-liner. – Antony Hatchkins Nov 16 '12 at 04:29
-
Yes, I mentioned python-colormath in my third comment to the question. – Antony Hatchkins Nov 16 '12 at 04:30
-
Anthony, however you are correct that it is not a single matrix, because the xyz->lab transform is defined differently in different ranges of xyz. Well, I don't think there's a one-liner, short of porting the relevant part of colormath to numpy first :) – Alex I Nov 16 '12 at 08:21
-
The question is not *if* there is a one-liner (two were given in my comments to the question) but rather *why* neither of those two found its way into PIL/numpy/scipy *or* if there're some better alternatives. – Antony Hatchkins Nov 16 '12 at 14:01
-
Also there's no need in 'porting' colormath to numpy as it uses numpy since its inception. – Antony Hatchkins Nov 16 '12 at 15:23
-
If you give an example of using pyCMS for this conversion, I shall accept your answer. – Antony Hatchkins Nov 16 '12 at 15:23
-
I'd delete the last part pertaining to PIL-only solution since it is incorrect. – Antony Hatchkins Nov 16 '12 at 20:33
-
pyCMS looks good but using it is risky -- seems it will not be well maintained. – fchen Feb 04 '13 at 23:30
-
After some research I significantly improved my answer and I feel that it should be accepted now. I compensated your reputation ;) – Antony Hatchkins Apr 18 '13 at 09:33
Here is a class, for transforming RGB<->LAB color spaces for PIL images:
from PIL import ImageCms
class ColorTrans:
'''Class for transforming RGB<->LAB color spaces for PIL images.'''
def __init__(self):
self.srgb_p = ImageCms.createProfile("sRGB")
self.lab_p = ImageCms.createProfile("LAB")
self.rgb2lab_trans = ImageCms.buildTransformFromOpenProfiles(srgb_p, lab_p, "RGB", "LAB")
self.lab2rgb_trans = ImageCms.buildTransformFromOpenProfiles(lab_p, srgb_p, "LAB", "RGB")
def rgb2lab(self, img):
return ImageCms.applyTransform(img, self.rgb2lab_trans)
def lab2rgb(self, img):
return ImageCms.applyTransform(img, self.lab2rgb_trans)
Sample usage:
color_trans = ColorTrans()
c_img = Image.open(FILENAME)
c_img_lab = color_trans.rgb2lab(c_img)
c_img_rgb = color_trans.lab2rgb(c_img_lab)

- 6,371
- 6
- 32
- 33
At the moment I haven't found a good package to do that. You have to bear in mind that RGB is a device-dependent colour space so you can't convert accurately to XYZ or CIE Lab if you don't have a profile.
So be aware that many solutions where you see converting from RGB to CIE Lab without specifying the colour space or importing a colour profile must be carefully evaluated. Take a look at the code under the hood most of the time they assume that you are dealing with sRGB colour space.

- 20,759
- 10
- 81
- 84