Solution 1:

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.

Solution 2:

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

Solution 3:

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/).

Solution 4:

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)