Spaces:
Sleeping
Sleeping
Commit
·
ec2b558
1
Parent(s):
bf0eeeb
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,18 @@ def hex_to_rgb(hex_code):
|
|
10 |
# Convert the hex code to RGB
|
11 |
return tuple(int(hex_code[i:i+2], 16) for i in (0, 2, 4))
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def resize_and_overlay_image(input_image, reduction_percentage, shift_pixels, shift_pixels_ud, background_color, crop_top_percentage, crop_bottom_percentage):
|
14 |
# Check if the input image is empty
|
15 |
if input_image.size == 0:
|
@@ -21,19 +33,18 @@ def resize_and_overlay_image(input_image, reduction_percentage, shift_pixels, sh
|
|
21 |
if img.ndim < 2:
|
22 |
return None
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
crop_bottom = int(height * crop_bottom_percentage / 100)
|
30 |
|
31 |
# Calculate the new dimensions based on the reduction percentage
|
32 |
new_height = int(height * reduction_percentage / 100)
|
33 |
new_width = int(width * reduction_percentage / 100)
|
34 |
|
35 |
-
# Resize the image
|
36 |
-
resized_img = cv2.resize(
|
37 |
|
38 |
# Convert the hex code to RGB
|
39 |
background_rgb = hex_to_rgb(background_color)
|
@@ -68,7 +79,7 @@ iface = gr.Interface(
|
|
68 |
gr.outputs.Textbox(label="Image Info")
|
69 |
],
|
70 |
title="Image Resizer",
|
71 |
-
description="
|
72 |
)
|
73 |
|
74 |
if __name__ == "__main__":
|
|
|
10 |
# Convert the hex code to RGB
|
11 |
return tuple(int(hex_code[i:i+2], 16) for i in (0, 2, 4))
|
12 |
|
13 |
+
def crop_image(image, crop_top_percentage, crop_bottom_percentage):
|
14 |
+
height, width = image.shape[:2]
|
15 |
+
|
16 |
+
# Calculate the number of pixels to crop from the top and bottom as a percentage of the image height
|
17 |
+
crop_top = int(height * crop_top_percentage / 100)
|
18 |
+
crop_bottom = int(height * crop_bottom_percentage / 100)
|
19 |
+
|
20 |
+
# Crop the input image
|
21 |
+
cropped_image = image[crop_top:height - crop_bottom, :]
|
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:
|
|
|
33 |
if img.ndim < 2:
|
34 |
return None
|
35 |
|
36 |
+
# Crop the input image
|
37 |
+
cropped_img = crop_image(img, crop_top_percentage, crop_bottom_percentage)
|
38 |
|
39 |
+
# Get the dimensions of the cropped image
|
40 |
+
height, width = cropped_img.shape[:2]
|
|
|
41 |
|
42 |
# Calculate the new dimensions based on the reduction percentage
|
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 |
|
49 |
# Convert the hex code to RGB
|
50 |
background_rgb = hex_to_rgb(background_color)
|
|
|
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__":
|