gaur3009 commited on
Commit
45a83bb
·
verified ·
1 Parent(s): 3847f5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -70
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 fit_design_to_target(design_img, target_img, x, y):
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
- # 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
- # Create a transparent overlay
17
- overlay = Image.new('RGBA', target_image.size, (255, 255, 255, 0))
18
- overlay.paste(design_image, (x, y), design_image)
19
 
20
- # Combine overlay with target image
21
- combined = Image.alpha_composite(target_image, overlay)
22
 
23
- return np.array(combined)
 
 
 
 
 
24
 
25
- with gr.Blocks() as editor:
26
- gr.Markdown("## Move and Fit Design on Dress Image")
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
- overlay_result = gr.Image(label="Resulting Image")
33
-
34
- # Hidden elements for coordinates
35
- x_coord = gr.Number(value=0, visible=False)
36
- y_coord = gr.Number(value=0, visible=False)
37
-
38
- apply_overlay = gr.Button("Apply Design to Dress")
39
-
40
- # JavaScript for click, drag, and drop
41
- custom_js = """
42
- <script>
43
- let designElement = null;
44
- let targetElement = null;
45
- let isDragging = false;
46
-
47
- function initDragAndDrop() {
48
- designElement = document.querySelector('img[data-target="design_img"]');
49
- targetElement = document.querySelector('img[data-target="target_img"]');
50
-
51
- designElement.style.position = 'absolute';
52
- designElement.style.cursor = 'move';
53
- designElement.draggable = true;
54
-
55
- designElement.ondragstart = function(event) {
56
- isDragging = true;
57
- };
58
-
59
- targetElement.ondragover = function(event) {
60
- event.preventDefault();
61
- };
62
-
63
- targetElement.ondrop = function(event) {
64
- if (isDragging) {
65
- let rect = targetElement.getBoundingClientRect();
66
- x_coord.value = event.clientX - rect.left;
67
- y_coord.value = event.clientY - rect.top;
68
- isDragging = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  }
70
- };
71
- }
72
-
73
- window.onload = initDragAndDrop;
74
- </script>
75
- """
76
-
77
- html_code = gr.HTML(custom_js)
78
 
79
- # Apply overlay based on click-and-drag action
80
- apply_overlay.click(fit_design_to_target, [design_img, target_img, x_coord, y_coord], overlay_result)
81
 
82
- editor.launch()
 
 
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()