# [Image-SIG] conversion of a full image from RGB<->HSI

Vinicius Cubas Brand viniciuscb at gmail.com
Tue Mar 15 21:02:31 CET 2005

```Hi.  Unfortunately I think that this transformation can not be done
with Image.convert: you can only do transformations between equivalent
color spaces (i.e. reach the destination colorspace by doing a
multiplication with a matrix with the source colorspace).

The HSI colorspace, instead, is not an equivalent colorspace of RGB.
To get the HSI equivalent of a RGB pixel one must do these
transformations:

I = 1/3 * (R+G+B)

S = 1 - (3/(R+G+B))*(min(R,G,B))

H = cos^-1 ( (((R-G)+(R-B))/2)/ (sqrt((R-G)^2 + (R-B)*(G-B) )))

Some references:
1: http://www.cs.rit.edu/~ncs/color/t_convert.html

There is a lot of efficient ways to do these transformations and also
efficient algorithms in C/C++ . But to do it in python pil i would
have to do a pixel by pixel operation with Image.getpixel and
Image.putpixel, with every image pixel, what seemed a very slow way to
do.

I already thought in making an extension to PIL to do this colorspace
transformation, but only asking to be sure there is not one already.

Vinicius

On Wed, 9 Mar 2005 17:50:25 +0100 (CET),
giovanniangeli at iquattrocastelli.it
<giovanniangeli at iquattrocastelli.it> wrote:
> > Hi all
> >
> > Can you help me in a doubt? Is there some efficient way to do a
> > conversion between an image in the RGB format in an image and the HSI
> > format (aka, HLS or HSV), in python (PIL or others)?
>
> yes, PIL gives that.
>
> look at the page
>
> http://www.pythonware.com/library/pil/handbook/image.htm
>
> in particular at the <convert> method of image Object in PIL.Image module.
> Enjoy.
>
> P.S. In general I have found in PIL an efficient and complete image
> processing library to be use from Python.
>
> Giovanni Angeli.
> <giovanniangeli at iquattrocastelli.it>
>
>

--
.----------------------.
| Vinicius Cubas Brand |
| viniciuscb gmail.com |
'----------------------'

"A human being is part of a whole, called by us the Universe, a part
limited in time and space. He experiences himself, his thoughts and
feelings, as something separated from the rest--a kind of optical
delusion of his consciousness. This delusion is a kind of prison for
us, restricting us to our personal desires and to affection for a few
persons nearest us. Our task must be to free ourselves from this
prison by widening our circles of compassion to embrace all living
creatures and the whole of nature in its beauty."
- Albert Einstein
```