Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,98 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
|
5 |
-
def
|
6 |
-
# Convert
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
# Calculate the area where the design should fit (full size of the dress image)
|
11 |
-
dress_area = target_image.size
|
12 |
-
|
13 |
-
# Resize the design image to fit within the dress area
|
14 |
-
design_image = design_image.resize(dress_area, Image.Resampling.LANCZOS)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
with gr.Row():
|
29 |
-
design_img = gr.Image(label="Design Image", type="numpy")
|
30 |
-
target_img = gr.Image(label="Dress Image", type="numpy")
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
}
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
</script>
|
75 |
-
"""
|
76 |
-
|
77 |
-
html_code = gr.HTML(custom_js)
|
78 |
|
79 |
-
|
80 |
-
apply_overlay.click(fit_design_to_target, [design_img, target_img, x_coord, y_coord], overlay_result)
|
81 |
|
82 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import cv2
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
|
6 |
+
def combine_images(dress_image, design_image, position, size):
|
7 |
+
# Convert images to numpy arrays
|
8 |
+
dress = np.array(dress_image)
|
9 |
+
design = np.array(design_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Convert position and size from strings to tuples of integers
|
12 |
+
x, y = map(int, position.split(","))
|
13 |
+
width, height = map(int, size.split(","))
|
14 |
|
15 |
+
# Resize design image to fit the size
|
16 |
+
design_resized = cv2.resize(design, (width, height))
|
17 |
|
18 |
+
# Create a mask for the design image
|
19 |
+
if design_resized.shape[2] == 4: # Handle transparency if PNG
|
20 |
+
design_mask = design_resized[:, :, 3] > 0
|
21 |
+
design_resized = design_resized[:, :, :3]
|
22 |
+
else:
|
23 |
+
design_mask = np.ones(design_resized.shape[:2], dtype=bool)
|
24 |
|
25 |
+
# Define region where the design will be placed
|
26 |
+
h, w = design_resized.shape[:2]
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Ensure the design fits within the dress image
|
29 |
+
if x + w > dress.shape[1]:
|
30 |
+
w = dress.shape[1] - x
|
31 |
+
design_resized = design_resized[:, :w]
|
32 |
+
design_mask = design_mask[:, :w]
|
33 |
+
|
34 |
+
if y + h > dress.shape[0]:
|
35 |
+
h = dress.shape[0] - y
|
36 |
+
design_resized = design_resized[:h]
|
37 |
+
design_mask = design_mask[:h]
|
38 |
+
|
39 |
+
# Place the design on the dress image
|
40 |
+
for c in range(3):
|
41 |
+
dress[y:y+h, x:x+w, c] = dress[y:y+h, x:x+w, c] * (1 - design_mask) + design_resized[:, :, c] * design_mask
|
42 |
+
|
43 |
+
return Image.fromarray(dress)
|
44 |
+
|
45 |
+
def interface():
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("## Image Editor with Drag-and-Drop")
|
48 |
+
dress_image = gr.Image(type="pil", label="Upload Dress Image")
|
49 |
+
design_image = gr.Image(type="pil", label="Upload Design Image", tool="editor") # Editor tool for resizing
|
50 |
+
|
51 |
+
position = gr.Textbox(placeholder="Enter position as x,y", value="100,100", label="Position")
|
52 |
+
size = gr.Textbox(placeholder="Enter size as width,height", value="200,200", label="Size")
|
53 |
+
|
54 |
+
btn = gr.Button("Fit Design")
|
55 |
+
output = gr.Image(type="pil", label="Final Output")
|
56 |
+
|
57 |
+
# Gradio button click function
|
58 |
+
btn.click(
|
59 |
+
combine_images,
|
60 |
+
inputs=[dress_image, design_image, position, size],
|
61 |
+
outputs=output
|
62 |
+
)
|
63 |
+
|
64 |
+
# Inject JavaScript for drag-and-drop functionality
|
65 |
+
gr.HTML("""
|
66 |
+
<script>
|
67 |
+
function initDragAndDrop() {
|
68 |
+
const designImg = document.querySelector('img[data-name="design_image"]');
|
69 |
+
let startX, startY, initialX, initialY;
|
70 |
+
|
71 |
+
designImg.addEventListener('mousedown', (e) => {
|
72 |
+
startX = e.clientX;
|
73 |
+
startY = e.clientY;
|
74 |
+
initialX = designImg.offsetLeft;
|
75 |
+
initialY = designImg.offsetTop;
|
76 |
+
|
77 |
+
document.onmousemove = (e) => {
|
78 |
+
const dx = e.clientX - startX;
|
79 |
+
const dy = e.clientY - startY;
|
80 |
+
designImg.style.position = 'absolute';
|
81 |
+
designImg.style.left = (initialX + dx) + 'px';
|
82 |
+
designImg.style.top = (initialY + dy) + 'px';
|
83 |
+
}
|
84 |
+
});
|
85 |
+
|
86 |
+
document.onmouseup = () => {
|
87 |
+
document.onmousemove = document.onmouseup = null;
|
88 |
+
}
|
89 |
}
|
90 |
+
|
91 |
+
window.onload = initDragAndDrop;
|
92 |
+
</script>
|
93 |
+
""")
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
return demo
|
|
|
96 |
|
97 |
+
demo = interface()
|
98 |
+
demo.launch()
|