Getting HSL Values

March 18, 2002 | Fredrik Lundh

Q. PIL can supply me with RGB data of an image. Does anybody know, how I can PIL have to transform this into the HSL (aka HSI) model?

PIL doesn’t support HLS images.

If you need HLS values for individual pixels, you can use the colorsys module in Python’s standard library:

 
>>> import Image
>>> im = Image.open("lenna.im")
>>> im.mode
'RGB'
>>> im.getpixel((0, 0))
(226, 161, 119)
>>> import colorsys
>>> colorsys.rgb_to_hls(*[x/255.0 for x in im.getpixel((0, 0))])
(0.065420560747663545, 0.67647058823529416, 0.64848484848484833)

Note that PIL returns RGB values in the range 0-255, while the colorsys module uses floating point values in the range 0.0-1.0.

 

A Django site. rendered by a django application. hosted by webfaction.