How could I convert RGB values to LAB values?

Hi, so I am very new to Python and I am trying to convert average RGB values of an image to LAB values so that I can use them within the Delta-E formula to determine the distance between colors. I am able to get the average RGB values fine, but once I run them into skimage.color.rgb2lab, the values I get are not accurate (ie. for RGB values of [122.81370300820946, 116.02862483728059, 102.01405734940582], I am getting LAB values of [76412.84670146, 80311.56527641, 66600.55043154]).

There are no other functions that could be manipulating the variables, so I am really confused as to how this is happening. Like I said I am new to Python so this is really confusing.

It is probably difficult for people to advise without a sample of code as you could be getting the wrong value for any number of reasons. Based on the massive values you are getting for the Lab values, I would guess you’ve scaled the values incorrectly. I assume you are using values in the range 0 - 255, but their convert function requires values in the range of 0 - 1. By default it appears to be using a D65 illuminant.

>>> import skimage
>>> skimage.color.rgb2lab([[[122.81370300820946 / 255, 116.02862483728059 / 255, 102.01405734940582 / 255]]])
array([[[49.07432939,  0.05027603,  8.68892207]]])

YOU ARE THE BEST THANK YOU SO MUCH! That was my problem, I did not know that you had to convert it to a range of 0-1, so I was using the whole values instead of dividing each value by 255, but that solved it. Thank you so much!

Use colorsys after reading the doc that says what value scaling to use for each color space.

1 Like

Per the documentation, colorsys does not understand the LAB colour space. It only understands Y’IQ (obsolete, according to a reference cited in the documentation! And that goes back to 2006!), HSV and HLS.

I think skimage handles most of the basic RGB color spaces and lab conversion. I imagine skimage is consistent with its requirements of value scaling as well, but If you are using something like PIL to read in your values from an image, the values are going to be returned in the range 0 - 255 by default, so stuff like this can occur if you are not careful.