Spaces:
Runtime error
Runtime error
import gradio as gr | |
from PIL import Image, ImageOps, ImageFilter | |
import cv2 | |
import numpy as np | |
def apply_filter(image, filter_type): | |
# Convert PIL image to OpenCV image | |
img = np.array(image.convert("RGB")) | |
if filter_type == "Grayscale": | |
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) | |
elif filter_type == "Invert": | |
img = cv2.bitwise_not(img) | |
elif filter_type == "Blur": | |
img = cv2.GaussianBlur(img, (15, 15), 0) | |
elif filter_type == "Edge Detection": | |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
edges = cv2.Canny(gray, 100, 200) | |
img = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB) | |
return Image.fromarray(img) | |
# Gradio UI | |
iface = gr.Interface( | |
fn=apply_filter, | |
inputs=[ | |
gr.Image(type="pil"), | |
gr.Radio(["Grayscale", "Invert", "Blur", "Edge Detection"], label="Choose a filter") | |
], | |
outputs=gr.Image(type="pil"), | |
title="🖼️ Image Filter App", | |
description="Upload an image and apply various filters!" | |
) | |
iface.launch() | |