You can download this code by clicking the button below.
This code is now available for download.
This function applies a color filter to an image based on the provided image path and filter name (red, green, blue).
Technology Stack : scikit-image, numpy
Code Type : Image processing
Code Difficulty : Intermediate
import numpy as np
from skimage import io, color, transform
def apply_color_filter(image_path, filter_name):
# Load the image
image = io.imread(image_path)
# Convert the image to HSV color space
hsv_image = color.rgb2hsv(image)
# Apply the color filter based on the filter_name
if filter_name == 'red':
hsv_image[:, :, 1] = hsv_image[:, :, 1] * 0.5 # Saturate the red channel
elif filter_name == 'green':
hsv_image[:, :, 0] = (hsv_image[:, :, 0] + np.pi) % (2 * np.pi) # Shift the green hue
elif filter_name == 'blue':
hsv_image[:, :, 2] = hsv_image[:, :, 2] * 0.5 # Desaturate the blue channel
else:
raise ValueError("Unsupported filter name. Use 'red', 'green', or 'blue'.")
# Convert the image back to RGB color space
filtered_image = color.hsv2rgb(hsv_image)
return filtered_image