gaur3009 commited on
Commit
3847f5b
·
verified ·
1 Parent(s): 71e6bed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -45
app.py CHANGED
@@ -2,13 +2,16 @@ import gradio as gr
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))
@@ -20,70 +23,60 @@ def overlay_images(design_img, target_img, x, y, width, height):
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")
27
- target_img = gr.Image(label="Target 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()
 
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))
 
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()