You can download this code by clicking the button below.
This code is now available for download.
This function accepts an image path, loads the image, and applies a random color filter. It first generates two random colors, then creates a mask containing the RGB components of these colors. Finally, it applies the filter by performing a bitwise AND operation between the mask and the original image.
Technology Stack : OpenCV, NumPy
Code Type : Image processing
Code Difficulty : Intermediate
import cv2
import numpy as np
def apply_random_color_filter(image_path):
# Load the image
image = cv2.imread(image_path)
# Generate random colors for the filter
random_color1 = np.random.randint(0, 255, 3)
random_color2 = np.random.randint(0, 255, 3)
# Create a mask for the filter
mask = np.zeros_like(image)
mask[:, :, 0] = random_color1[0]
mask[:, :, 1] = random_color2[1]
mask[:, :, 2] = random_color1[2]
# Apply the color filter
filtered_image = cv2.bitwise_and(image, mask)
return filtered_image