Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
|
| 5 |
+
def create_canvas(width, height, bg_color):
|
| 6 |
+
# Create a new image with the specified size and background color
|
| 7 |
+
img = Image.new('RGB', (width, height), color=bg_color)
|
| 8 |
+
return np.array(img)
|
| 9 |
+
|
| 10 |
+
def add_shape(image, shape_type, x, y, size, color):
|
| 11 |
+
img = Image.fromarray(image)
|
| 12 |
+
draw = ImageDraw.Draw(img)
|
| 13 |
+
|
| 14 |
+
if shape_type == "square":
|
| 15 |
+
draw.rectangle([x, y, x+size, y+size], fill=color)
|
| 16 |
+
elif shape_type == "circle":
|
| 17 |
+
draw.ellipse([x, y, x+size, y+size], fill=color)
|
| 18 |
+
elif shape_type == "rectangle":
|
| 19 |
+
draw.rectangle([x, y, x+size, y+size//2], fill=color)
|
| 20 |
+
|
| 21 |
+
return np.array(img)
|
| 22 |
+
|
| 23 |
+
def update_canvas(image, width, height, bg_color, shape_type, x, y, size, color):
|
| 24 |
+
# Create new canvas if size changed
|
| 25 |
+
if image.shape[0] != height or image.shape[1] != width:
|
| 26 |
+
image = create_canvas(width, height, bg_color)
|
| 27 |
+
|
| 28 |
+
# Add shape to the canvas
|
| 29 |
+
image = add_shape(image, shape_type, x, y, size, color)
|
| 30 |
+
|
| 31 |
+
return image
|
| 32 |
+
|
| 33 |
+
# Define the Gradio interface
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# Image Generator")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
width = gr.Slider(256, 1024, 256, step=1, label="Canvas Width")
|
| 39 |
+
height = gr.Slider(256, 1024, 256, step=1, label="Canvas Height")
|
| 40 |
+
bg_color = gr.ColorPicker(label="Background Color", value="#ffffff")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
shape_type = gr.Dropdown(["square", "circle", "rectangle"], label="Shape Type")
|
| 44 |
+
x = gr.Slider(0, 1000, 0, step=1, label="X Position")
|
| 45 |
+
y = gr.Slider(0, 1000, 0, step=1, label="Y Position")
|
| 46 |
+
size = gr.Slider(10, 200, 50, step=1, label="Shape Size")
|
| 47 |
+
color = gr.ColorPicker(label="Shape Color")
|
| 48 |
+
|
| 49 |
+
canvas = gr.Image(shape=(256, 256), image_mode="RGB")
|
| 50 |
+
|
| 51 |
+
add_btn = gr.Button("Add Shape")
|
| 52 |
+
|
| 53 |
+
add_btn.click(
|
| 54 |
+
update_canvas,
|
| 55 |
+
inputs=[canvas, width, height, bg_color, shape_type, x, y, size, color],
|
| 56 |
+
outputs=canvas
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|