Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,25 +2,25 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
|
5 |
-
def
|
6 |
-
# Convert to PIL images
|
7 |
design_image = Image.fromarray(design_img).convert("RGBA")
|
8 |
target_image = Image.fromarray(target_img).convert("RGBA")
|
9 |
|
10 |
-
# Resize the design image
|
11 |
-
design_image = design_image.resize((
|
12 |
-
|
13 |
# Create a transparent overlay
|
14 |
overlay = Image.new('RGBA', target_image.size, (255, 255, 255, 0))
|
15 |
-
overlay.paste(design_image, (
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
return np.array(
|
21 |
|
22 |
with gr.Blocks() as editor:
|
23 |
-
gr.Markdown("## Drag, Drop, and Resize
|
24 |
|
25 |
with gr.Row():
|
26 |
design_img = gr.Image(label="Design Image", type="numpy")
|
@@ -28,83 +28,62 @@ with gr.Blocks() as editor:
|
|
28 |
|
29 |
overlay_result = gr.Image(label="Overlay Result")
|
30 |
|
31 |
-
# Hidden elements
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
apply_overlay_btn = gr.Button("Apply Overlay")
|
38 |
|
39 |
-
#
|
|
|
|
|
|
|
40 |
custom_js = """
|
41 |
<script>
|
42 |
-
let
|
43 |
-
let
|
44 |
-
let resultImg = document.querySelector('img[data-target="overlay_result"]');
|
45 |
-
|
46 |
-
let finalX = document.querySelector('input[name="final_x"]');
|
47 |
-
let finalY = document.querySelector('input[name="final_y"]');
|
48 |
-
let finalWidth = document.querySelector('input[name="final_width"]');
|
49 |
-
let finalHeight = document.querySelector('input[name="final_height"]');
|
50 |
-
|
51 |
-
let active = false;
|
52 |
-
let currentX, currentY, initialX, initialY;
|
53 |
-
let offsetX = 0, offsetY = 0;
|
54 |
-
|
55 |
-
designImg.style.position = "absolute";
|
56 |
-
designImg.style.cursor = "move";
|
57 |
-
designImg.style.zIndex = "10";
|
58 |
|
59 |
-
function
|
60 |
-
|
61 |
-
|
62 |
-
initialY = e.clientY - offsetY;
|
63 |
-
}
|
64 |
-
|
65 |
-
function dragEnd() {
|
66 |
-
active = false;
|
67 |
-
offsetX = currentX;
|
68 |
-
offsetY = currentY;
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
function drag(e) {
|
77 |
-
if (active) {
|
78 |
-
e.preventDefault();
|
79 |
-
|
80 |
-
currentX = e.clientX - initialX;
|
81 |
-
currentY = e.clientY - initialY;
|
82 |
-
|
83 |
-
designImg.style.transform = `translate3d(${currentX}px, ${currentY}px, 0)`;
|
84 |
-
}
|
85 |
-
}
|
86 |
-
|
87 |
-
designImg.addEventListener("mousedown", dragStart);
|
88 |
-
window.addEventListener("mouseup", dragEnd);
|
89 |
-
window.addEventListener("mousemove", drag);
|
90 |
-
|
91 |
-
designImg.addEventListener("wheel", (e) => {
|
92 |
-
e.preventDefault();
|
93 |
-
|
94 |
-
let newWidth = designImg.width + (e.deltaY < 0 ? 10 : -10);
|
95 |
-
let newHeight = designImg.height + (e.deltaY < 0 ? 10 : -10);
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
100 |
}
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
</script>
|
103 |
"""
|
104 |
|
105 |
html_code = gr.HTML(custom_js)
|
106 |
|
107 |
-
#
|
108 |
-
|
109 |
|
110 |
editor.launch()
|
|
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
|
5 |
+
def overlay_images(design_img, target_img, x, y, width, height):
|
6 |
+
# Convert inputs to PIL images
|
7 |
design_image = Image.fromarray(design_img).convert("RGBA")
|
8 |
target_image = Image.fromarray(target_img).convert("RGBA")
|
9 |
|
10 |
+
# Resize the design image
|
11 |
+
design_image = design_image.resize((width, height), Image.Resampling.LANCZOS)
|
12 |
+
|
13 |
# Create a transparent overlay
|
14 |
overlay = Image.new('RGBA', target_image.size, (255, 255, 255, 0))
|
15 |
+
overlay.paste(design_image, (x, y), design_image)
|
16 |
+
|
17 |
+
# Combine overlay with target image
|
18 |
+
combined = Image.alpha_composite(target_image, overlay)
|
19 |
+
|
20 |
+
return np.array(combined)
|
21 |
|
22 |
with gr.Blocks() as editor:
|
23 |
+
gr.Markdown("## Drag, Drop, and Resize the Design Image on the Target Image")
|
24 |
|
25 |
with gr.Row():
|
26 |
design_img = gr.Image(label="Design Image", type="numpy")
|
|
|
28 |
|
29 |
overlay_result = gr.Image(label="Overlay Result")
|
30 |
|
31 |
+
# Hidden elements for storing coordinates and size
|
32 |
+
x_coord = gr.Number(value=0, visible=False)
|
33 |
+
y_coord = gr.Number(value=0, visible=False)
|
34 |
+
width = gr.Number(value=100, visible=False)
|
35 |
+
height = gr.Number(value=100, visible=False)
|
|
|
|
|
36 |
|
37 |
+
# Button to apply the overlay
|
38 |
+
apply_overlay = gr.Button("Apply Overlay")
|
39 |
+
|
40 |
+
# JavaScript for dragging, resizing, and positioning
|
41 |
custom_js = """
|
42 |
<script>
|
43 |
+
let designElement = null;
|
44 |
+
let targetElement = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
function initDragAndResize() {
|
47 |
+
designElement = document.querySelector('img[data-target="design_img"]');
|
48 |
+
targetElement = document.querySelector('img[data-target="target_img"]');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
let overlay = document.createElement('div');
|
51 |
+
overlay.style.position = 'absolute';
|
52 |
+
overlay.style.border = '2px dashed #000';
|
53 |
+
overlay.style.cursor = 'move';
|
54 |
+
overlay.style.resize = 'both';
|
55 |
+
overlay.style.overflow = 'auto';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
targetElement.parentNode.appendChild(overlay);
|
58 |
+
|
59 |
+
// Drag functionality
|
60 |
+
overlay.onmousedown = function(event) {
|
61 |
+
document.onmousemove = function(event) {
|
62 |
+
overlay.style.left = event.pageX - overlay.offsetWidth / 2 + 'px';
|
63 |
+
overlay.style.top = event.pageY - overlay.offsetHeight / 2 + 'px';
|
64 |
+
}
|
65 |
}
|
66 |
+
|
67 |
+
document.onmouseup = function() {
|
68 |
+
document.onmousemove = null;
|
69 |
+
}
|
70 |
+
|
71 |
+
// Save the position and size when the user applies the overlay
|
72 |
+
applyOverlay.onclick = function() {
|
73 |
+
x_coord.value = parseInt(overlay.style.left) - targetElement.getBoundingClientRect().left;
|
74 |
+
y_coord.value = parseInt(overlay.style.top) - targetElement.getBoundingClientRect().top;
|
75 |
+
width.value = overlay.offsetWidth;
|
76 |
+
height.value = overlay.offsetHeight;
|
77 |
+
}
|
78 |
+
}
|
79 |
+
|
80 |
+
window.onload = initDragAndResize;
|
81 |
</script>
|
82 |
"""
|
83 |
|
84 |
html_code = gr.HTML(custom_js)
|
85 |
|
86 |
+
# Load the overlayed image on clicking the button
|
87 |
+
apply_overlay.click(overlay_images, [design_img, target_img, x_coord, y_coord, width, height], overlay_result)
|
88 |
|
89 |
editor.launch()
|