I want to convert RGB values to HSV using python. I got some code samples, which gave the result with the S and V values greater than 100. (example : http://code.activestate.com/recipes/576554-covert-color-space-from-hsv-to-rgb-and-rgb-to-hsv/ ) . anybody got a better code which convert RGB to HSV and vice versa
thanks
If using PIL, with a recent copy of Pillow, one should probably use
Based on array indexing and slicing in numpy, this is my approach for the forward:
and backward color space conversion:
I was motivated to write these lines as I wasn't convinced by a pixel-wise conversion due to the computational overload and also did not want to rely on another library such as OpenCV.
Feel free to suggest a modification to make this solution more elegant and generic.
I suggest to work with OpenCV
I've looked at the various libraries from opencv, colorsys and PIL.
colorsys is not very accurate but gets within 15 degrees. Most of the time within 3 degress. FOr the others there seems to be minor dependencies over a few degrees, but who is more correct? I don't know. But here is my numpyfied rgb to hsv code. I think it's pretty good but I am sure it could be better optimized somehow. The most obvious would be to combine the H, s, and v. Yes that is there also. Mine's more accurate then colorsys for sure, but you definitely see some small rounding errors. But hey it does the whole array in several steps at least. Sorry no invert code. Maybe another day I will write it. Ok so last thing is I don't assume scale. You have to add your own post scaling for the separate hue, sat, and val. the optional s argument is input scaling to 1, the dhue, dsat and dval are out scaling. And yes this is for 2dim arrays. it shouldn't be hard to fix it to work on while, or just reshape your stuff to 2dim and then back again.
Did you try using the colorsys library?
Example (taken from the above link):
What values of R, G, and B did you put in, and what values of H, S, and V that look erroneous did you get out?
The code you link to, like colorsys, expects float values between 0.0 and 1.0. If you call it with integer values in the 0-to-255 range, you'll get bogus results. It should work fine if you give it the input values it expects.
(It is a distinct failure of that code sample that it does not actually document what sorts of inputs it expects.)