How to convert an HSV color to RGB in Python [4 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# Table of Contents

  1. How to convert an HSV color to RGB in Python
  2. Dealing with rounding errors when converting HSV to RGB
  3. Convert an HSV color to a non-normalized RGB color in Python
  4. Convert an HSV color to RGB using matplotlib

# How to convert an HSV color to RGB in Python

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.

main.py
import colorsys rgb = colorsys.hsv_to_rgb(0.2, 0.3, 0.5) print(rgb) # ๐Ÿ‘‰๏ธ (0.47, 0.5, 0.35)

python convert hsv color to rgb

The code for this article is available on GitHub

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.

main.py
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)

convert hsv color to rgb and vice versa

Notice that you might run into small rounding errors because of how floating-point math works in Python.

# Dealing with rounding errors when converting HSV to RGB

If you want to handle rounding errors when converting HSV to RGB, you can round the RGB coordinates to 2 or more decimal places.

main.py
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))

rounding rgb coordinates to deal with rounding errors

The code for this article is available on GitHub

The hsv_to_rgb() function takes the HSV hue, saturation and value as parameters and rounds the returned RGB coordinates to 2 decimal places.

# How to convert an HSV color to a non-normalized RGB color in Python

If you want to convert an HSV color to a non-normalized RGB color, multiply each RGB coordinate by 255.

main.py
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))

convert hsv color to non normalized rgb color

Each primary color is represented by a value between 0 and 255.

0 is the minimum intensity and 255 is the maximum.

# How to convert an HSV color to RGB using matplotlib

You can also use the matplotlib module to convert an HSV color to RGB.

First, make sure you have the matplotlib module installed.

shell
pip install matplotlib numpy pip3 install matplotlib numpy

Now, import and use the matplotlib.colors.hsv_to_rgb method.

main.py
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]

convert hsv color to rgb using matplotlib

The code for this article is available on GitHub

The matplotlib.colors.hsv_to_rgb() method converts HSV values to RGB.

The method takes an array-like object where all values are assumed to be in the range from 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.

main.py
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)

passing two dimensional numpy array to the method

If you don't use NumPy, pass a native Python list to the matplotlib.colors.hsv_to_rgb() method.

main.py
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]

pass native python list to the method

You can also pass a two-dimensional list to the method.

main.py
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)

pass two dimensional list to the method

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev