RGB To HSV in Python using colorsys

60 Views Asked by At

To covert RGB To HSV, I used this code:

import colorsys
def RGB_To_HSV(Red: int, Green: int, Blue: int):
    for I in colorsys.rgb_to_hsv(Red / 255, Green / 255, Blue / 255):
        H, S, V = round(360*I[0]), round(100*I[1]), round(100*I[2])
    return (H, S, V)

print(RGB_To_HSV(255,127,0))

But I get an error saying that:

Traceback (most recent call last):
    File "C:\Users\User\Desktop\GHJ.py", line 17, in <module>
        print(RGB_To_HSV(255,127,0))
    File "C:\Users\User\Desktop\GHJ.py", line 14, in RGB_To_HSV
        H, S, V = round(360*I[0]), round(100*I[1]), round(100*I[2])
TypeError: 'float' object is not subscriptable

How do I fix this?

1

There are 1 best solutions below

0
Talha Tayyab On BEST ANSWER
import colorsys
print(colorsys.rgb_to_hsv(255 / 255, 127 / 255, 0 / 255))

#output
(0.08300653594771241, 1.0, 1.0)

If you do a forloop you will get individual float values:

for I in colorsys.rgb_to_hsv(255 / 255, 127 / 255, 0 / 255):
    print(I)

#output
0.08300653594771241
1.0
1.0

You need to remove your forloop

import colorsys
def RGB_To_HSV(Red: int, Green: int, Blue: int):
    I = colorsys.rgb_to_hsv(Red / 255, Green / 255, Blue / 255)
    H, S, V = round(360*I[0]), round(100*I[1]), round(100*I[2])
    return (H, S, V)

print(RGB_To_HSV(255,127,0))

#output
(30, 100, 100)

Related Questions in COLORSYS