| Reference | Downloads | Github

RGB to cielab conversion

I would like to specify the colors in my experiment in cieLAB because I think that is what the “color world” wants. I am using a mac, a lot of the experimental structure is in builder but there are plenty of code segments, so I am not afraid of writing a little Python.

I found:
from skimage import io, color
which has conversion functions, but I am not sure how I download it so Psychopy can use it, or if there is another package I should use instead of skimage. With Psychopy I can easily convert to the systems it uses (RGB, HSV, etc), but I want to start with cieLAB, convert to RGB or whatever, and use Psychpy to do the presentation.

BTW Happy holidays to all: (Late Hanuka, Christmas; today Kwanzaa, Saturnian; and if I forgot your holiday have a good time anyway.


I think it would simply require converting the Lab value to RGB and setting it as the stimuli’s color property, as long as the colorspace is RGB. You would need to re-scale the values between -1.0 and 1.0 if the returned values are between 0 and 255 (or whatever).

I got that, RGB have to be scaled from 0 to 1.
I’ve tried to install skimage with no luck. Is it compatible with PsychoPy? I can download
scikit-image-0.13.1.tar.gz and unpack it but I do not know where to put the files. I am using a Mac using OSX10.12.6. I am using the PsychoPy interface (not anacondra, etc yet). My experiments are a mixture of builder with often a bunch of code components.

The basic problem is to go from CIELAB to RGB. There are two version of CIELAB: Cartesian and Cylindrical. I am pretty sure I can go from one to the other with simple formula. But going back and forth between CIELAB to one of the representations used by PsychoPy, I am lost. . skimage seemed like one way but maybe there are simpler ways.


Adding external modules to Standalone PsychoPy:

Thank you again Michael. you guys have thought of everything. So far I haven’t gotten the skimage package to work but it possible that I don’t understand the package well enough and i have not tried the 'Adding a .pth file’ method. But I just started working on it and at least i have something to go on. (It might be that the skimage has so many requirements that it will not work. I will work on it.

Meanwhile I have found a python listing of rgb2lab which is the opposite of that I need and a java script that does both, plus something called python-colormath. I will give it all a try. Maybe when I finish, if the solutions are good, I will post them here. My aim is to use the cieLAB cylindrical representation if I can so its like our familiar HSL.

Thanks, now at least I know how to add libraries to PsychoPy. You guys have thought of about everything.

1 Like

Hi Bill,

If you find yourself needing to install lots of external libraries and dependencies, you might find it more practical to install your own version of Python (Anaconda is a very good scientifically-oriented one Anaconda | Individual Edition) and install PsychoPy into that (i.e. PsychoPy itself just becomes another Python module, rather than being a stand-alone application that contains its own complete Python distribution).

Then PsychoPy will have access to other libraries with a simple import statement.

Anaconda makes it very easy to switch between multiple environments (e.g. you can simultaneously have Python 2 and 3 installed). It also has a very good package manager called conda. Anaconda comes with a lot of those scientific packages already, but if not, it could be installed from the terminal with a simple command like this:

conda install scikit-image

This would also install any needed dependencies for you, taking account of what you already have installed. Then that library would be immediately available to your PsychoPy scripts with a simple import

I got it. In a posting somewhere Jon mentioned that PsychoPy had a
cielab2rgb(lab) command. I don’t program (use builder when I can get away with it) but I just copied Jon’s example (which is in the code). I want to go from Lch cie to RGB, but the transformation from Lch to lab I found is trivial:
rgb = cielab2rgb(lab) #Its a bit more complex, see example in code listing in colorspacetools.
I wasted a lot of time, but got it done and I get the correct rgb values out (D65) so (-:
For those that wonder, Lch is a transformation of cieL
B that makes hue circular, and the other 2 dimensions correspond roughly to saturation and lightness.


The CIELAB stuff is new to PsychoPy 1.9, so it’ll take some time to make it into Builder.

Note that the returned values are in linear RGB space. If your display is not gamma corrected, the LAB colours will appear darker than expected. There are some transfer functions in colorspacetools to convert linear RGB to sRGB that can be supplied to the transferFunc argument of cielab2rgb.

I got calibrating monitor finally figured out, I think. There is a call that returns 4 parameters (a,b,k, and gamma). I had difficulty accessing the “hard (4 parameter)” monitor setting but did finally make it work. Unfortunately we our lab moved so I have to do it over. I am 70 years old and since high school have learned 7 computer languages. I can write bits of python, so I am not only builder. But I don’t want to start learning another language. For 10 years I used Objective C (terrible, Apple only) and then they changed language. I would be for a constitutional amendment not to invent more languages!

My suggestion is to list all the transformation PsychoPy does next time you get to upgrading that part of documentation. I might add Lch (Hcl?).

Thanks, it was a long road.

I’ll certainly consider adding that conversion too (I added the CIELab* conversion in response to your original post, then completely forgot to tell anyone about it). I agree we’ll need to figure out what to do about documentation. I’ve been using IDEs which pull docstrings from function definitions, but we should get the HTML docs updated.

Wow, thats terrific. I spent a lot of time first translating java code (a language I do not know) to python (a language I am unfortunately learning), and now its all done. Oh well, the hard part Jon (or someone) did for me. And my two versions (4 lines in psychoPy) and ~70 from Java to Python give the same results, so I am cool with it all. Thank you for the many times you have help me.