Spaces:
Build error

stickman / app.py
akashdeb's picture
Create app.py
edce1a7 verified
Raw History Blame Contribute Delete
2.23 kB
import gradio as gr
from PIL import Image, ImageDraw
# Store joints globally for simplicity
joints = []
# Function to load image and display it
def upload_image(image):
global joints
joints = [] # Reset joints
return image
# Function to add joints
def add_joint(image, x, y):
global joints
joints.append((x, y))
img = Image.open(image).convert("RGB")
draw = ImageDraw.Draw(img)
for joint in joints:
draw.ellipse([joint[0]-5, joint[1]-5, joint[0]+5, joint[1]+5], fill="red")
return img
# Function to move a joint
def move_joint(image, index, new_x, new_y):
global joints
if 0 <= index < len(joints):
joints[index] = (new_x, new_y)
img = Image.open(image).convert("RGB")
draw = ImageDraw.Draw(img)
for joint in joints:
draw.ellipse([joint[0]-5, joint[1]-5, joint[0]+5, joint[1]+5], fill="red")
return img
# Function to download the modified image
def download_image(image):
img = Image.open(image)
return img
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("## Stickfigure Manipulator")
with gr.Row():
with gr.Column():
img_input = gr.Image(label="Upload Stickfigure", type="filepath")
upload_btn = gr.Button("Upload")
x_coord = gr.Slider(0, 500, label="X Coordinate")
y_coord = gr.Slider(0, 500, label="Y Coordinate")
add_btn = gr.Button("Add Joint")
joint_index = gr.Number(label="Joint Index")
new_x = gr.Slider(0, 500, label="New X Coordinate")
new_y = gr.Slider(0, 500, label="New Y Coordinate")
move_btn = gr.Button("Move Joint")
download_btn = gr.Button("Download Image")
with gr.Column():
img_output = gr.Image(label="Output")
# Function Bindings
upload_btn.click(upload_image, inputs=[img_input], outputs=[img_output])
add_btn.click(add_joint, inputs=[img_input, x_coord, y_coord], outputs=[img_output])
move_btn.click(move_joint, inputs=[img_input, joint_index, new_x, new_y], outputs=[img_output])
download_btn.click(download_image, inputs=[img_input], outputs=[gr.File()])
# Run the app
if __name__ == "__main__":
demo.launch()