155elkhorn commited on
Commit
a20a37c
·
1 Parent(s): 6bbb984

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -47,21 +47,26 @@ def resize_and_overlay_image(input_image, reduction_percentage, shift_percentage
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
-
53
- # Convert the hex code to RGB
54
- background_rgb = hex_to_rgb(background_color)
 
 
 
 
55
 
56
  # Create a new background image with the original image dimensions and specified color
 
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
65
 
66
  # Return the resulting image as a NumPy array
67
  return background_img, f"Input Image Dimensions: {width}x{height}"
@@ -83,7 +88,7 @@ iface = gr.Interface(
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__":
 
47
  shift_x = int(width * shift_percentage_lr / 100)
48
  shift_y = int(height * shift_percentage_ud / 100)
49
 
50
+ # Resize the cropped image while maintaining aspect ratio
51
+ aspect_ratio = width / height
52
+ if new_width / new_height > aspect_ratio:
53
+ resized_height = new_height
54
+ resized_width = int(new_height * aspect_ratio)
55
+ else:
56
+ resized_width = new_width
57
+ resized_height = int(new_width / aspect_ratio)
58
+ resized_img = cv2.resize(cropped_img, (resized_width, resized_height))
59
 
60
  # Create a new background image with the original image dimensions and specified color
61
+ background_rgb = hex_to_rgb(background_color)
62
  background_img = np.ones((height, width, 3), dtype=np.uint8) * background_rgb
63
 
64
  # Calculate the position to overlay the resized image on the background image
65
+ x = int((width - resized_width) / 2) + shift_x
66
+ y = shift_y
67
 
68
  # Overlay the resized image on the background image
69
+ background_img[y:y + resized_height, x:x + resized_width] = resized_img
70
 
71
  # Return the resulting image as a NumPy array
72
  return background_img, f"Input Image Dimensions: {width}x{height}"
 
88
  gr.outputs.Textbox(label="Image Info")
89
  ],
90
  title="Image Resizer",
91
+ description="Crop the input image, overlay it on a new background of the specified color, start centered, and shift it as a percentage without stretching."
92
  )
93
 
94
  if __name__ == "__main__":