Convert RGB image color with HSV values

In Image, The RGB color model stands for red, green, and blue light, which are added together in different ways to reproduce a wide array of colors of this particular image. On the other hand, the HSV color model represents colors in terms of their shade (hue), intensity (saturation), and brightness (value). It is often used in color selection tools to perceive and describe colors.

Required Python Libary:

  • pip install opencv-python
  • pip install numpy

Example 1:

This code should work as expected to read an image, modify its HSV values, display the modified image, and save it. Ensure that the image file path (img_path) is correct and that you have permission to read from and write to the directories involved.

Python
import cv2
import numpy as np

def modify_hsv_image(image_path, hue_change=0, saturation_change=0, value_change=0, display_result=True):
    
    original_img = cv2.imread(image_path)
    
    if original_img is None:
        raise FileNotFoundError(f"Error: Could not read image from {image_path}")

    # Convert the image from BGR to HSV
    hsv_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2HSV)

    # Apply hue change (wrap around for values outside 0-179)
    hsv_img[..., 0] = (hsv_img[..., 0] + hue_change) % 180

    # Apply saturation and value changes with clipping (0-255)
    hsv_img[..., 1] = np.clip(hsv_img[..., 1] + saturation_change, 0, 255)
    hsv_img[..., 2] = np.clip(hsv_img[..., 2] + value_change, 0, 255)

    bgr_img_new = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)

    if display_result:
        cv2.imshow("Covered_modified Image", bgr_img_new)
        cv2.imwrite('Covered_modified_image.jpg', bgr_img_new)
        cv2.waitKey(0)  
        cv2.destroyAllWindows()

    return bgr_img_new

if __name__ == "__main__":
    img_path = "seabeach.jpg"
    hue_change=4
    saturation_change=10
    value_change=3
    try:
        modified_image = modify_hsv_image(img_path,hue_change ,saturation_change ,value_change)
    except FileNotFoundError as e:
        print(e)

Before:

Suppose, you have normal image name, “seabeach.jpg” like this:

After changing the HSV value by code, the “Covered_modified_seabeach_image.jpg” look like this:

After:

Example 2:

Suppose, you have normal image name, “bird.jpg”, In this case, we have changed the HUE, SATURATION AND VALUE.

Python
import cv2
import numpy as np

def modify_hsv_image(image_path, hue_change=0, saturation_change=0, value_change=0, display_result=True):
    
    original_img = cv2.imread(image_path)
    
    if original_img is None:
        raise FileNotFoundError(f"Error: Could not read image from {image_path}")

    # Convert the image from BGR to HSV
    hsv_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2HSV)

    # Apply hue change (wrap around for values outside 0-179)
    hsv_img[..., 0] = (hsv_img[..., 0] + hue_change) % 180

    # Apply saturation and value changes with clipping (0-255)
    hsv_img[..., 1] = np.clip(hsv_img[..., 1] + saturation_change, 0, 255)
    hsv_img[..., 2] = np.clip(hsv_img[..., 2] + value_change, 0, 255)

    bgr_img_new = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)

    if display_result:
        cv2.imshow("Covered_modified_bird Image", bgr_img_new)
        cv2.imwrite('Covered_modified_bird_image.jpg', bgr_img_new)
        cv2.waitKey(0)  
        cv2.destroyAllWindows()

    return bgr_img_new

if __name__ == "__main__":
    img_path = "bird.jpg"
    hue_change=10
    saturation_change=15
    value_change=5
    try:
        modified_image = modify_hsv_image(img_path,hue_change ,saturation_change ,value_change)
    except FileNotFoundError as e:
        print(e)

Before:

After:




Change RGB image color with HSV values with Python OpenCV

An open-source library of Python, OpenCV is mainly used for image and video processing. On the other hand, there is also open source Python Library NumPy, short for Numerical Python, which works with large arrays. In this article, we will show you How we can Change RGB image color with HSV values with OpenCV using Python.

Similar Reads

Convert RGB image color with HSV values

In Image, The RGB color model stands for red, green, and blue light, which are added together in different ways to reproduce a wide array of colors of this particular image. On the other hand, the HSV color model represents colors in terms of their shade (hue), intensity (saturation), and brightness (value). It is often used in color selection tools to perceive and describe colors....