Skip to main content

How to Convert HSV to RGB in Python: colorsys and matplotlib

HSV (Hue, Saturation, Value) and RGB (Red, Green, Blue) are different color models.

This guide explains how to convert colors from HSV to RGB in Python using the built-in colorsys module and, as an alternative, the matplotlib.colors module. We'll also cover how to handle potential rounding errors and how to get non-normalized RGB values (0-255 range).

The colorsys module is part of the Python standard library, so you don't need to install anything extra. It provides functions for converting between different color systems.

import colorsys

rgb = colorsys.hsv_to_rgb(0.2, 0.3, 0.5) # H, S, V values (all between 0.0 and 1.0)
print(rgb) # Output: (0.47, 0.5, 0.35)
  • colorsys.hsv_to_rgb(h, s, v): Converts HSV coordinates to RGB coordinates.
    • h (hue): Represents the color itself (e.g., red, green, blue). It's an angle on the color wheel, typically ranging from 0 to 1 (where 0 and 1 both represent red). You can also think of it as a value between 0 and 360 degrees, if you convert it to degrees.
    • s (saturation): Represents the intensity of the color (0.0 is grayscale, 1.0 is fully saturated).
    • v (value/brightness): Represents the brightness of the color (0.0 is black, 1.0 is full brightness).
note
  • The h, s, and v values must be floats between 0.0 and 1.0 (inclusive). The returned RGB values are also floats between 0.0 and 1.0.
  • colorsys also provides the inverse function colorsys.rgb_to_hsv().

Handling Rounding Errors

Floating-point arithmetic can sometimes introduce tiny rounding errors. If you need precise decimal values, round the results:

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) # Round to 2 decimal places

print(colorsys.hsv_to_rgb(0.3, 0.4, 0.6)) # Output: (0.40800000000000003, 0.6, 0.36)
print(hsv_to_rgb(0.3, 0.4, 0.6)) # Output: (0.41, 0.6, 0.36)
  • A function that converts HSV coordinates to RGB coordinates.
  • round(x, 2) rounds to two decimal places.
  • A generator expression tuple(round(x, 2) for x in rgb) is used to efficiently transform all the RGB values.

Getting Non-Normalized RGB (0-255)

Standard RGB values for display or image processing are often in the range 0-255 (integers). To get these values, multiply each component of the result by 255 and round:

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) # Multiply by 255 and round

print(hsv_to_rgb(0.3, 0.4, 0.6)) # Output: (104, 153, 92)

Converting HSV to RGB with matplotlib

If you're already using matplotlib for plotting, you can use its hsv_to_rgb function. You'll need to install matplotlib and numpy (pip install matplotlib numpy).

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) # Output: [0.47 0.5 0.35]

# You can also pass a 2D array:
hsv_array_2d = np.array([
[0.2, 0.3, 0.5],
[0.2, 0.4, 0.6],
])
rgb_2d = hsv_to_rgb(hsv_array_2d)
print(rgb_2d)

# Output:
# [[0.47 0.5 0.35 ]
# [0.552 0.6 0.36 ]]

# Also works with Python lists:
hsv_list = [0.2, 0.3, 0.5]
rgb_from_list = hsv_to_rgb(hsv_list) # Works with list input
print(rgb_from_list) # Output: [0.47 0.5 0.35]
note
  • matplotlib.colors.hsv_to_rgb works with both lists and NumPy arrays.
  • This is convenient if you're already using matplotlib, but it adds an external dependency if you're not. colorsys is built-in and generally preferred for simple conversions.