Last updated: Apr 13, 2024
Reading timeยท3 min
Use the colorsys.hsv_to_rgb()
method to convert an HSV color to RGB in
Python.
The colorsys.hsv_to_rgb()
method takes the hue, saturation and value as
parameters and converts from HSV coordinates to RGB coordinates.
import colorsys rgb = colorsys.hsv_to_rgb(0.2, 0.3, 0.5) print(rgb) # ๐๏ธ (0.47, 0.5, 0.35)
Make sure to import the built-in colorsys
module before calling
colorsys.hsv_to_rgb()
.
The three arguments we passed to the colorsys.hsv_to_rgb module are the hue, saturation and value.
The method converts the given HSV coordinates to RGB coordinates.
There is also a colorsys.rgb_to_hsv() method that is used to convert the given RGB coordinates to HSV coordinates.
import colorsys rgb = colorsys.hsv_to_rgb(0.2, 0.3, 0.5) print(rgb) # ๐๏ธ (0.47, 0.5, 0.35) hsv = colorsys.rgb_to_hsv(*rgb) # ๐๏ธ (0.20000000000000004, 0.30000000000000004, 0.5) print(hsv)
Notice that you might run into small rounding errors because of how floating-point math works in Python.
If you want to handle rounding errors when converting HSV to RGB, you can round the RGB coordinates to 2 or more decimal places.
import colorsys def hsv_to_rgb(hue, saturation, value): rgb = colorsys.hsv_to_rgb(hue, saturation, value) return tuple(round(x, 2) for x in rgb) # ๐๏ธ (0.40800000000000003, 0.6, 0.36) print(colorsys.hsv_to_rgb(0.3, 0.4, 0.6)) # ๐๏ธ (0.41, 0.6, 0.36) print(hsv_to_rgb(0.3, 0.4, 0.6))
The hsv_to_rgb()
function takes the HSV hue, saturation and value as
parameters and rounds the returned RGB coordinates to 2 decimal places.
If you want to convert an HSV color to a non-normalized RGB color, multiply each RGB coordinate by 255.
import colorsys def hsv_to_rgb(hue, saturation, value): rgb = colorsys.hsv_to_rgb(hue, saturation, value) return tuple(round(x * 255) for x in rgb) # ๐๏ธ (104, 153, 92) print(hsv_to_rgb(0.3, 0.4, 0.6))
Each primary color is represented by a value between 0
and 255
.
0
is the minimum intensity and 255
is the maximum.
matplotlib
You can also use the matplotlib
module to convert an HSV color to RGB.
First, make sure you
have the matplotlib
module installed.
pip install matplotlib numpy pip3 install matplotlib numpy
Now, import and use the matplotlib.colors.hsv_to_rgb method.
import numpy as np from matplotlib.colors import hsv_to_rgb hsv_array = np.array([0.2, 0.3, 0.5]) rgb = hsv_to_rgb(hsv_array) print(rgb) # ๐๏ธ [0.47 0.5 0.35]
The matplotlib.colors.hsv_to_rgb()
method converts HSV values to RGB.
0
to 1
.The method returns an ndarray
with the colors converted to RGB values in the
range from 0
to 1
.
You can also pass a two-dimensional NumPy array to the method.
import numpy as np from matplotlib.colors import hsv_to_rgb hsv_array = np.array([ [0.2, 0.3, 0.5], [0.2, 0.4, 0.6], ]) rgb = hsv_to_rgb(hsv_array) # [[0.47 0.5 0.35 ] # [0.552 0.6 0.36 ]] print(rgb)
If you don't use NumPy, pass a native Python list to the
matplotlib.colors.hsv_to_rgb()
method.
from matplotlib.colors import hsv_to_rgb hsv_list = [0.2, 0.3, 0.5] rgb = hsv_to_rgb(hsv_list) print(rgb) # ๐๏ธ [0.47 0.5 0.35]
You can also pass a two-dimensional list to the method.
from matplotlib.colors import hsv_to_rgb hsv_list = [ [0.2, 0.3, 0.5], [0.2, 0.4, 0.6], ] rgb = hsv_to_rgb(hsv_list) # [[0.47 0.5 0.35 ] # [0.552 0.6 0.36 ]] print(rgb)
You can learn more about the related topics by checking out the following tutorials: