''' author : OzlemAkgunoglu github : https://github.com/OzlemAkgunoglu Dynamic Photo Filter App This is a Dynamic Photo Filter App that allows you to apply various filters to your images. Adjust brightness, contrast, sharpening, and select a filter for real-time changes. And this app is created using OpenCV and Gradio. Thank you for using it. ''' #Let's load the necessary libraries import cv2 as cv #OpenCV for image processing import numpy as np #Numpy for arrays import gradio as gr #Gradio for UI # Let's define the filter functions def apply_grayscale(image): return cv.cvtColor(image, cv.COLOR_BGR2GRAY) #Convert the image to grayscale #Sepia filter function def apply_sepia(image): sepia_filter = np.array([[0.272, 0.534, 0.131], [0.349, 0.686, 0.168], [0.393, 0.769, 0.189]]) sepia_image = cv.transform(image, sepia_filter) #Apply the filter return np.clip(sepia_image, 0, 255).astype(np.uint8) #clip to hold values between 0 and 255 prevent excessive brightness or darkening. def apply_negative(image): return cv.bitwise_not(image) #Invert the image #sketch filter '''Apply Gaussian blur to decrease the noise and remove unwanted details in the image for better sketch effect ''' def apply_sketch(image): gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) #Convert the image to grayscale inv = cv.bitwise_not(gray) #Invert the grayscale image blurred = cv.GaussianBlur(inv, (21, 21), sigmaX=0, sigmaY=0) sketch_image = cv.divide(gray, 255 - blurred, scale=256) # divide= gray / (255 - blurred) Normalizes the division result to prevent overly high values by scaling with 256 return sketch_image def apply_sharpen(image, sharpening): sharpening_filter = np.array([[0, -1, 0], [-1, 5 + sharpening, -1], [0, -1, 0]]) return cv.filter2D(image, -1, sharpening_filter) #Apply the filter each pixel is multiplied by the value in the kernel def apply_edge_detection(image): return cv.Canny(image, 100, 200) def apply_fall_filter(frame): fall_filter = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]) return cv.transform(frame, fall_filter) # Dictionary to map filter names to functions filter_functions = { "Grayscale": apply_grayscale, "Sepia": apply_sepia, "Negative": apply_negative, "Sketch": apply_sketch, "Sharpen": apply_sharpen, "Edge Detection": apply_edge_detection, "Fall": apply_fall_filter } # Main function to apply selected filters def apply_filters(image, filter_type, brightness, contrast, sharpening): if image is None: print("Input image is empty!") #for debugging return None # Return None if the input image is empty # Adjust brightness and contrast image = cv.convertScaleAbs(image, alpha=contrast, beta=brightness) #for debugging #print("Image after brightness and contrast adjustment:", image) # Apply the selected filter from dictionary called filter_functions in line 53 if filter_type in filter_functions: if filter_type == "Sharpen": image = filter_functions[filter_type](image, sharpening) # Calls the Sharpen filter with the sharpening parameter for custom sharpening level else: image = filter_functions[filter_type](image) return image # Define Interface with gr.Blocks() as app: # Title and Description gr.Markdown("