Change RGB image color with HSV values with Python OpenCV
Last Updated :
05 Jun, 2024
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.
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:

Similar Reads
Convert BGR and RGB with Python - OpenCV Prerequisites: OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human
2 min read
Adding borders to the images using Python - OpenCV Image processing is an interesting field in today's era of Artificial Intelligence and Machine Learning. We can see the applications of image processing in our day-to-day life, like whenever we apply filter over any image (selfie) or when we want to apply some effect like blurring the image, etc. I
1 min read
Splitting and Merging Channels with Python-OpenCV In this article, we will learn how to split a multi-channel image into separate channels and combine those separate channels into a multi-channel image using OpenCV in Python. To do this, we use cv2.split() and cv2.merge() functions respectively. Image Used: Splitting Channels cv2.split() is used
2 min read
Detect the RGB color from a webcam using Python - OpenCV Prerequisites: Python NumPy, Python OpenCVEvery image is represented by 3 colors that are Red, Green and Blue. Let us see how to find the most dominant color captured by the webcam using Python.Approach:Import the cv2 and NumPy modulesCapture the webcam video using the cv2.VideoCapture(0) method.Dis
2 min read
Program to Change RGB color model to HSV color model Given RGB color range, our task is to convert RGB color to HSV color.RGB Color Model : The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three
9 min read