155elkhorn commited on
Commit
124faa0
·
1 Parent(s): fdff301

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -22,7 +22,7 @@ def crop_image(image, crop_top_percentage, crop_bottom_percentage):
22
 
23
  return cropped_image
24
 
25
- def resize_and_overlay_image(input_image, reduction_percentage, shift_pixels, shift_pixels_ud, background_color, crop_top_percentage, crop_bottom_percentage):
26
  # Check if the input image is empty
27
  if input_image.size == 0:
28
  return None
@@ -43,6 +43,10 @@ def resize_and_overlay_image(input_image, reduction_percentage, shift_pixels, sh
43
  new_height = int(height * reduction_percentage / 100)
44
  new_width = int(width * reduction_percentage / 100)
45
 
 
 
 
 
46
  # Resize the cropped image
47
  resized_img = cv2.resize(cropped_img, (new_width, new_height))
48
 
@@ -53,8 +57,8 @@ def resize_and_overlay_image(input_image, reduction_percentage, shift_pixels, sh
53
  background_img = np.ones((height, width, 3), dtype=np.uint8) * background_rgb
54
 
55
  # Calculate the position to overlay the resized image on the background image
56
- x = int((width - new_width) / 2) + int(shift_pixels)
57
- y = int((height - new_height) / 2) + int(shift_pixels_ud)
58
 
59
  # Overlay the resized image on the background image
60
  background_img[y:y + new_height, x:x + new_width] = resized_img
@@ -68,20 +72,19 @@ iface = gr.Interface(
68
  inputs=[
69
  gr.inputs.Image(type="pil", label="Input Image"),
70
  gr.inputs.Slider(minimum=0, maximum=100, step=10, default=80, label="Percentage of Original"),
71
- gr.inputs.Slider(minimum=-150, maximum=150, step=10, default=0, label="Shift Pixels Left / Right"),
72
- gr.inputs.Slider(minimum=-150, maximum=250, step=10, default=0, label="Shift Pixels Up / Down"),
73
  gr.inputs.Textbox(default="#ffffff", label="Background Color (Hex Code)"),
74
- gr.inputs.Slider(minimum=0, maximum=100, step=5, default=0, label="Crop Top (%)"),
75
- gr.inputs.Slider(minimum=0, maximum=100, step=5, default=0, label="Crop Bottom (%)")
76
  ],
77
  outputs=[
78
  gr.outputs.Image(type="numpy", label="Result"),
79
  gr.outputs.Textbox(label="Image Info")
80
  ],
81
  title="Image Resizer",
82
- description="Crop the input image, overlay it on a new background of the specified color, shift it, and resize it as a percentage."
83
  )
84
 
85
  if __name__ == "__main__":
86
  iface.launch()
87
-
 
22
 
23
  return cropped_image
24
 
25
+ def resize_and_overlay_image(input_image, reduction_percentage, shift_percentage_lr, shift_percentage_ud, background_color, crop_top_percentage, crop_bottom_percentage):
26
  # Check if the input image is empty
27
  if input_image.size == 0:
28
  return None
 
43
  new_height = int(height * reduction_percentage / 100)
44
  new_width = int(width * reduction_percentage / 100)
45
 
46
+ # Calculate the shift values as a percentage of the image dimensions
47
+ shift_x = int(width * shift_percentage_lr / 100)
48
+ shift_y = int(height * shift_percentage_ud / 100)
49
+
50
  # Resize the cropped image
51
  resized_img = cv2.resize(cropped_img, (new_width, new_height))
52
 
 
57
  background_img = np.ones((height, width, 3), dtype=np.uint8) * background_rgb
58
 
59
  # Calculate the position to overlay the resized image on the background image
60
+ x = int((width - new_width) / 2) + shift_x
61
+ y = int((height - new_height) / 2) + shift_y
62
 
63
  # Overlay the resized image on the background image
64
  background_img[y:y + new_height, x:x + new_width] = resized_img
 
72
  inputs=[
73
  gr.inputs.Image(type="pil", label="Input Image"),
74
  gr.inputs.Slider(minimum=0, maximum=100, step=10, default=80, label="Percentage of Original"),
75
+ gr.inputs.Slider(minimum=-50, maximum=50, step=10, default=0, label="Shift Left / Right (%)"),
76
+ gr.inputs.Slider(minimum=-50, maximum=50, step=10, default=0, label="Shift Up / Down (%)"),
77
  gr.inputs.Textbox(default="#ffffff", label="Background Color (Hex Code)"),
78
+ gr.inputs.Slider(minimum=0, maximum=100, step=10, default=0, label="Crop Top (%)"),
79
+ gr.inputs.Slider(minimum=0, maximum=100, step=10, default=0, label="Crop Bottom (%)")
80
  ],
81
  outputs=[
82
  gr.outputs.Image(type="numpy", label="Result"),
83
  gr.outputs.Textbox(label="Image Info")
84
  ],
85
  title="Image Resizer",
86
+ description="Crop the input image, overlay it on a new background of the specified color, shift it as a percentage, and resize it as a percentage."
87
  )
88
 
89
  if __name__ == "__main__":
90
  iface.launch()