⚡ Computer Vision Workflows in Gradio
The gr.Image component in Gradio supports image uploads, drag-and-drop zones, sketch pads, and live webcam streaming out of the box. By combining it with NumPy, Pillow, or OpenCV, developers can build photo editors, filters, face detectors, and webcam analysis pipelines instantly.
Example 1: Live Webcam Snapshot & Grayscale Converter
Captures live frames from a connected webcam and converts them to black-and-white images using NumPy.
Python (vision_ex1.py)
import gradio as gr
import numpy as np
def webcam_grayscale(image):
if image is None:
return None
# Convert RGB to Grayscale by averaging color channels
gray = np.mean(image, axis=2).astype(np.uint8)
return gray
demo = gr.Interface(
fn=webcam_grayscale,
inputs=gr.Image(sources=["webcam", "upload"], label="Webcam Stream"),
outputs=gr.Image(label="Grayscale Output"),
title="Live Webcam Grayscale Filter"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Live Webcam Grayscale Filter
Webcam Stream Source
📷 Live Camera Stream
Grayscale Output
⬛ Grayscale Preview
Example 2: Image Brightness & Contrast Adjuster
Applies brightness intensity adjustments to uploaded photographs using PIL ImageEnhance.
Python (vision_ex2.py)
import gradio as gr
from PIL import Image, ImageEnhance
import numpy as np
def adjust_brightness(image_np, factor):
img = Image.fromarray(image_np.astype('uint8'))
enhancer = ImageEnhance.Brightness(img)
return np.array(enhancer.enhance(factor))
demo = gr.Interface(
fn=adjust_brightness,
inputs=[gr.Image(label="Source Image"), gr.Slider(0.1, 3.0, value=1.2, label="Brightness Factor")],
outputs=gr.Image(label="Enhanced Image"),
title="Image Brightness Studio"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Image Brightness Studio
Source Image Canvas
Brightness Factor: [----●----] (1.2)
Example 3: Image Rotation & Geometric Flips
Rotates uploaded images dynamically based on user-selected orientation angles.
Python (vision_ex3.py)
import gradio as gr
from PIL import Image
import numpy as np
def rotate_image(image_np, angle):
img = Image.fromarray(image_np.astype('uint8'))
rotated = img.rotate(int(angle), expand=True)
return np.array(rotated)
demo = gr.Interface(
fn=rotate_image,
inputs=[gr.Image(label="Upload Image"), gr.Radio([0, 90, 180, 270], value=90, label="Rotation Angle (°)")],
outputs=gr.Image(label="Rotated Result"),
title="Geometric Image Rotator"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Geometric Image Rotator
Rotation Angle (°): ◎ 0 ● 90 ○ 180 ○ 270
Rotated Image Canvas Preview
Example 4: Photo Negative / Color Inversion Filter
Inverts RGB pixel color values to generate photographic negative effects.
Python (vision_ex4.py)
import gradio as gr
import numpy as np
def invert_colors(image_np):
return 255 - image_np
demo = gr.Interface(
fn=invert_colors,
inputs=gr.Image(label="Original Photo"),
outputs=gr.Image(label="Inverted Negative"),
title="Instant Color Negative Generator"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Instant Color Negative Generator
Original Photo
[Upload Canvas]
Inverted Negative
[Inverted Output]
Example 5: Image Cropping & Resizing Studio
Resizes uploaded images to standard target resolutions using Pillow resampling.
Python (vision_ex5.py)
import gradio as gr
from PIL import Image
import numpy as np
def resize_image(image_np, width, height):
img = Image.fromarray(image_np.astype('uint8'))
resized = img.resize((int(width), int(height)))
return np.array(resized)
demo = gr.Interface(
fn=resize_image,
inputs=[gr.Image(label="Source Image"), gr.Slider(100, 1000, value=300, label="Target Width"), gr.Slider(100, 1000, value=300, label="Target Height")],
outputs=gr.Image(label="Resized Image"),
title="Batch Image Resizer"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Batch Image Resizer
Target Width: [---●-----] (300px)
Target Height: [---●-----] (300px)
Example 6: Custom Text Watermark Overlay
Overlays custom copyright strings onto photographs using Pillow drawing modules.
Python (vision_ex6.py)
import gradio as gr
from PIL import Image, ImageDraw
import numpy as np
def apply_watermark(image_np, text):
img = Image.fromarray(image_np.astype('uint8')).convert("RGBA")
draw = ImageDraw.Draw(img)
draw.text((20, 20), text, fill=(255, 255, 255, 200))
return np.array(img.convert("RGB"))
demo = gr.Interface(
fn=apply_watermark,
inputs=[gr.Image(label="Upload Image"), gr.Textbox(label="Watermark Text", value="© 2026 Nikita Infotech")],
outputs=gr.Image(label="Watermarked Image"),
title="Professional Photo Watermarker"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Professional Photo Watermarker
Watermark Text: © 2026 Nikita Infotech
Watermarked Photo Canvas Preview
Example 7: Edge Detection Filter (Sobel / Mock Filter)
Extracts structural outlines and boundaries from input photographs using image processing algorithms.
Python (vision_ex7.py)
import gradio as gr
from PIL import Image, ImageOps
import numpy as np
def detect_edges(image_np):
img = Image.fromarray(image_np.astype('uint8')).convert("L")
edges = ImageOps.invert(img).point(lambda p: 255 if p > 100 else 0)
return np.array(edges.convert("RGB"))
demo = gr.Interface(
fn=detect_edges,
inputs=gr.Image(label="Source Image"),
outputs=gr.Image(label="Edge Sketch Output"),
title="Outline & Edge Detector"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Outline & Edge Detector
Source Image
[Input Photo]
Edge Sketch Output
[Outline Sketch]
Example 8: Image Blur & Gaussian Smoothing Studio
Applies Gaussian blur smoothing effects with adjustable kernel radius parameters.
Python (vision_ex8.py)
import gradio as gr
from PIL import Image, ImageFilter
import numpy as np
def apply_blur(image_np, radius):
img = Image.fromarray(image_np.astype('uint8'))
blurred = img.filter(ImageFilter.GaussianBlur(radius=int(radius)))
return np.array(blurred)
demo = gr.Interface(
fn=apply_blur,
inputs=[gr.Image(label="Source Image"), gr.Slider(0, 15, value=3, label="Blur Radius")],
outputs=gr.Image(label="Blurred Result"),
title="Gaussian Blur Filter Studio"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Gaussian Blur Filter Studio
Blur Radius: [----●----] (3)
Blurred Photo Preview
Example 9: Color Channel Splitter (RGB Extraction)
Extracts and isolates individual Red, Green, and Blue color channels from photographs.
Python (vision_ex9.py)
import gradio as gr
import numpy as np
def extract_channel(image_np, channel):
out = np.zeros_like(image_np)
idx = {"Red": 0, "Green": 1, "Blue": 2}[channel]
out[:, :, idx] = image_np[:, :, idx]
return out
demo = gr.Interface(
fn=extract_channel,
inputs=[gr.Image(label="Source Image"), gr.Radio(["Red", "Green", "Blue"], value="Red", label="Isolate Channel")],
outputs=gr.Image(label="Channel Output"),
title="RGB Channel Extractor"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### RGB Channel Extractor
Isolate Channel: ● Red ○ Green ○ Blue
Red Channel Isolated Preview
Example 10: Real-Time Webcam Filter Studio (`gr.Blocks` + Webcam)
Combines live webcam streaming blocks with selectable filter controls inside an interactive dashboard.
Python (vision_ex10.py)
import gradio as gr
import numpy as np
def apply_effect(cam_img, effect):
if cam_img is None:
return None
if effect == "Grayscale":
return np.mean(cam_img, axis=2).astype(np.uint8)
elif effect == "Negative":
return 255 - cam_img
return cam_img
with gr.Blocks() as demo:
gr.Markdown("# 📷 Live Webcam Effects Studio")
with gr.Row():
cam = gr.Image(sources=["webcam"], label="Webcam Feed")
preview = gr.Image(label="Processed Stream")
mode = gr.Radio(["Normal", "Grayscale", "Negative"], value="Grayscale", label="Select Effect")
cam.change(apply_effect, inputs=[cam, mode], outputs=preview)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
# 📷 Live Webcam Effects Studio
Webcam Feed
Live Feed Active
Processed Stream
Filtered Feed
Select Effect: ● Grayscale ○ Negative ○ Normal