Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert RGB values to equivalent HSV values using python

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

like image 850
Sreejith Sasidharan Avatar asked Apr 10 '10 05:04

Sreejith Sasidharan


People also ask

What is HSV color in Python?

HSV - (hue, saturation, value), also known as HSB (hue, saturation, brightness), is often used by artists because it is often more natural to think about a color in terms of hue and saturation than in terms of additive or subtractive color components.

What is HSV vs RGB?

HSV is a cylindrical color model that remaps the RGB primary colors into dimensions that are easier for humans to understand. Like the Munsell Color System, these dimensions are hue, saturation, and value. Hue specifies the angle of the color on the RGB color circle.


2 Answers

Did you try using the colorsys library?

The colorsys module defines bidirectional conversions of color values between colors expressed in the RGB (Red Green Blue) color space used in computer monitors and three other coordinate systems: YIQ, HLS (Hue Lightness Saturation) and HSV (Hue Saturation Value)

Example (taken from the above link):

>>> import colorsys
>>> colorsys.rgb_to_hsv(.3, .4, .2)
(0.25, 0.5, 0.4)
>>> colorsys.hsv_to_rgb(0.25, 0.5, 0.4)
(0.3, 0.4, 0.2)
like image 110
Eli Bendersky Avatar answered Sep 23 '22 14:09

Eli Bendersky


If using PIL, with a recent copy of Pillow, one should probably use

def rgb2hsv(image):
    return image.convert('HSV')
like image 41
K3---rnc Avatar answered Sep 20 '22 14:09

K3---rnc