Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RGB to HSV wrong in colorsys?

Tags:

python

rgb

hsv

I'm using the colorsys lib of Python:

import colorsys
colorsys.rgb_to_hsv(64, 208, 61)

output:(0.16666666666666666, 0, 208)

But this output is wrong, this is the true value using a RGB to HSV online converter: RGB to HSV

What's going on?

like image 605
Jota Avatar asked Feb 04 '23 21:02

Jota


1 Answers

colorsys takes its values in the range 0 to 1:

Coordinates in all of these color spaces are floating point values. In the YIQ space, the Y coordinate is between 0 and 1, but the I and Q coordinates can be positive or negative. In all other spaces, the coordinates are all between 0 and 1.

You need to divide each of the values by 255. to get the expected output:

>>> colorsys.rgb_to_hsv(64/255., 208/255., 61/255.)
(0.3299319727891157, 0.7067307692307692, 0.8156862745098039)
like image 125
Dan D. Avatar answered Feb 07 '23 11:02

Dan D.