Spaces:
Running
Running
import gradio as gr | |
import numpy as np | |
def dot_effect(input_image, dot_size, threshold, dot_color): | |
""" | |
Convert input image to customizable dotted effect | |
Args: | |
input_image (ndarray): Input image | |
dot_size (int): Size of dots | |
threshold (float): Brightness threshold for dot appearance | |
dot_color (list): RGB color of dots | |
Returns: | |
ndarray: Image with dotted effect | |
""" | |
# Convert input to grayscale | |
gray_image = np.mean(input_image, axis=2) | |
# Normalize pixel values | |
normalized = (gray_image - gray_image.min()) / (gray_image.max() - gray_image.min()) | |
# Create dot mask | |
height, width = gray_image.shape | |
dot_mask = np.zeros((height, width, 3), dtype=np.uint8) | |
for y in range(0, height, dot_size): | |
for x in range(0, width, dot_size): | |
if normalized[y, x] > threshold: | |
radius = int(dot_size * normalized[y, x]) | |
center_x, center_y = x + dot_size//2, y + dot_size//2 | |
for dy in range(-radius, radius): | |
for dx in range(-radius, radius): | |
if dx*dx + dy*dy <= radius*radius: | |
px, py = center_x + dx, center_y + dy | |
if 0 <= px < width and 0 <= py < height: | |
dot_mask[py, px] = dot_color | |
return dot_mask | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=dot_effect, | |
inputs=[ | |
gr.Image(type="numpy", label="Input Image"), | |
gr.Slider(minimum=1, maximum=20, value=5, label="Dot Size"), | |
gr.Slider(minimum=0, maximum=1, value=0.3, label="Dot Threshold"), | |
gr.ColorPicker(label="Dot Color", value="#FFFFFF") | |
], | |
outputs=gr.Image(type="numpy"), | |
title="ChatGPT Ad Maker", | |
description="Transform images into customizable dotted effect" | |
) | |
# Launch the app | |
iface.launch() |