Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image, ImageOps
|
5 |
+
|
6 |
+
# Initialize global variables
|
7 |
+
design_image = None
|
8 |
+
target_image = None
|
9 |
+
|
10 |
+
def load_images(design_path, target_path):
|
11 |
+
global design_image, target_image
|
12 |
+
design_image = Image.open(design_path).convert("RGBA")
|
13 |
+
target_image = Image.open(target_path).convert("RGBA")
|
14 |
+
return design_image, target_image
|
15 |
+
|
16 |
+
def overlay_images(drag_info):
|
17 |
+
if not drag_info or not design_image or not target_image:
|
18 |
+
return target_image
|
19 |
+
|
20 |
+
# Extract drag position and scale
|
21 |
+
x = drag_info['x']
|
22 |
+
y = drag_info['y']
|
23 |
+
scale = drag_info['scale']
|
24 |
+
|
25 |
+
# Resize the design image
|
26 |
+
resized_design = design_image.resize(
|
27 |
+
(int(design_image.width * scale), int(design_image.height * scale)),
|
28 |
+
Image.ANTIALIAS
|
29 |
+
)
|
30 |
+
|
31 |
+
# Overlay design onto target
|
32 |
+
overlay = Image.new('RGBA', target_image.size, (255, 255, 255, 0))
|
33 |
+
overlay.paste(resized_design, (x, y), resized_design)
|
34 |
+
combined_image = Image.alpha_composite(target_image, overlay)
|
35 |
+
|
36 |
+
return combined_image
|
37 |
+
|
38 |
+
# Gradio Interface
|
39 |
+
with gr.Blocks() as editor:
|
40 |
+
gr.Markdown("## Drag-and-Drop Image Editor")
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
with gr.Column():
|
44 |
+
design_path = gr.Image(label="Design Image")
|
45 |
+
target_path = gr.Image(label="Target Image")
|
46 |
+
load_btn = gr.Button("Load Images")
|
47 |
+
with gr.Column():
|
48 |
+
drag_info = gr.JSON(label="Drag Info", value={"x": 0, "y": 0, "scale": 1.0})
|
49 |
+
drag_area = gr.Canvas(tool="drag", label="Drag Design Here", width=600, height=400)
|
50 |
+
overlay_result = gr.Image(label="Overlay Result")
|
51 |
+
|
52 |
+
load_btn.click(load_images, inputs=[design_path, target_path], outputs=[drag_info, drag_area])
|
53 |
+
drag_area.edit(overlay_images, inputs=drag_info, outputs=overlay_result)
|
54 |
+
|
55 |
+
editor.launch()
|