gaur3009 commited on
Commit
ccdbd4d
·
verified ·
1 Parent(s): 04baf73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -41
app.py CHANGED
@@ -2,31 +2,25 @@ import gradio as gr
2
  import numpy as np
3
  from PIL import Image
4
 
5
- def overlay_images(x, y, scale, design_img, target_img):
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 based on scale
11
- design_image = design_image.resize(
12
- (int(design_image.width * scale), int(design_image.height * scale)), Image.Resampling.LANCZOS
13
- )
14
-
15
- # Ensure the design image has an alpha channel for transparency
16
- if design_image.mode != 'RGBA':
17
- design_image = design_image.convert('RGBA')
18
 
19
  # Create a transparent overlay
20
  overlay = Image.new('RGBA', target_image.size, (255, 255, 255, 0))
21
- overlay.paste(design_image, (x, y), design_image)
22
-
23
- # Combine overlay with target image
24
- combined = Image.alpha_composite(target_image, overlay)
25
 
26
- return np.array(combined)
 
 
 
27
 
28
  with gr.Blocks() as editor:
29
- gr.Markdown("## Drag-and-Drop Image Editor with Resizing")
30
 
31
  with gr.Row():
32
  design_img = gr.Image(label="Design Image", type="numpy")
@@ -34,43 +28,83 @@ with gr.Blocks() as editor:
34
 
35
  overlay_result = gr.Image(label="Overlay Result")
36
 
37
- # Hidden elements to hold drag-drop coordinates and scale
38
- x_coord = gr.Number(value=0, visible=False)
39
- y_coord = gr.Number(value=0, visible=False)
40
- scale_factor = gr.Number(value=1.0, visible=False)
 
41
 
42
- apply_overlay = gr.Button("Apply Overlay")
43
 
44
- # JavaScript for dragging and resizing
45
  custom_js = """
46
  <script>
47
- let img = document.querySelector('img[data-target="design_img"]');
48
- let result = document.querySelector('img[data-target="overlay_result"]');
49
- let xInput = document.querySelector('input[name="x_coord"]');
50
- let yInput = document.querySelector('input[name="y_coord"]');
51
- let scaleInput = document.querySelector('input[name="scale_factor"]');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- img.draggable = true;
54
- img.ondragstart = function(event) {
55
- event.dataTransfer.setDragImage(new Image(), 0, 0);
56
- };
 
 
 
 
 
 
57
 
58
- img.ondragend = function(event) {
59
- let rect = result.getBoundingClientRect();
60
- xInput.value = event.clientX - rect.left;
61
- yInput.value = event.clientY - rect.top;
62
- };
63
 
64
- let resizeObserver = new ResizeObserver(entries => {
65
- scaleInput.value = img.width / img.naturalWidth;
 
 
 
 
 
 
 
 
66
  });
67
- resizeObserver.observe(img);
68
  </script>
69
  """
70
 
71
  html_code = gr.HTML(custom_js)
72
 
73
- # Load the overlayed image on clicking the button
74
- apply_overlay.click(overlay_images, [x_coord, y_coord, scale_factor, design_img, target_img], overlay_result)
75
 
76
  editor.launch()
 
2
  import numpy as np
3
  from PIL import Image
4
 
5
+ def apply_overlay(final_x, final_y, final_width, final_height, design_img, target_img):
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 based on the final dimensions
11
+ design_image = design_image.resize((final_width, final_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, (final_x, final_y), design_image)
 
 
 
16
 
17
+ # Merge overlay with target image
18
+ combined_image = Image.alpha_composite(target_image, overlay)
19
+
20
+ return np.array(combined_image)
21
 
22
  with gr.Blocks() as editor:
23
+ gr.Markdown("## Drag, Drop, and Resize Your Design")
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 to store the final positioning and size after user interaction
32
+ final_x = gr.Number(value=0, visible=False)
33
+ final_y = gr.Number(value=0, visible=False)
34
+ final_width = gr.Number(value=100, visible=False)
35
+ final_height = gr.Number(value=100, visible=False)
36
 
37
+ apply_overlay_btn = gr.Button("Apply Overlay")
38
 
39
+ # JavaScript code for dragging, resizing, and cropping
40
  custom_js = """
41
  <script>
42
+ let designImg = document.querySelector('img[data-target="design_img"]');
43
+ let targetImg = document.querySelector('img[data-target="target_img"]');
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 dragStart(e) {
60
+ active = true;
61
+ initialX = e.clientX - offsetX;
62
+ initialY = e.clientY - offsetY;
63
+ }
64
+
65
+ function dragEnd() {
66
+ active = false;
67
+ offsetX = currentX;
68
+ offsetY = currentY;
69
+
70
+ finalX.value = offsetX;
71
+ finalY.value = offsetY;
72
+ finalWidth.value = designImg.width;
73
+ finalHeight.value = designImg.height;
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
+ if (newWidth > 50 && newHeight > 50) {
98
+ designImg.style.width = `${newWidth}px`;
99
+ designImg.style.height = `${newHeight}px`;
100
+ }
101
  });
 
102
  </script>
103
  """
104
 
105
  html_code = gr.HTML(custom_js)
106
 
107
+ # Apply overlay on clicking the button
108
+ apply_overlay_btn.click(apply_overlay, [final_x, final_y, final_width, final_height, design_img, target_img], overlay_result)
109
 
110
  editor.launch()