Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,45 +16,33 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
| 16 |
|
| 17 |
# Function to add padding
|
| 18 |
def fill_rectangle_cropper(img, padding_type):
|
| 19 |
-
#
|
| 20 |
avg_color_per_row = np.average(np.array(img), axis=0)
|
| 21 |
avg_color = np.average(avg_color_per_row, axis=0)
|
| 22 |
|
| 23 |
if padding_type == "top_bottom":
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return newimg
|
| 35 |
-
else:
|
| 36 |
-
print("No padding needed for top/bottom.") # Debug message
|
| 37 |
-
return img
|
| 38 |
|
| 39 |
elif padding_type == "left_right":
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return newimg
|
| 51 |
-
else:
|
| 52 |
-
print("No padding needed for left/right.") # Debug message
|
| 53 |
-
return img
|
| 54 |
-
|
| 55 |
-
else:
|
| 56 |
-
print("Invalid padding type.") # Debug message
|
| 57 |
-
return img
|
| 58 |
|
| 59 |
# Function for cropping and filling the image
|
| 60 |
def fill_rectangle_cropper1(img):
|
|
|
|
| 16 |
|
| 17 |
# Function to add padding
|
| 18 |
def fill_rectangle_cropper(img, padding_type):
|
| 19 |
+
# Calculate the average color of the image
|
| 20 |
avg_color_per_row = np.average(np.array(img), axis=0)
|
| 21 |
avg_color = np.average(avg_color_per_row, axis=0)
|
| 22 |
|
| 23 |
if padding_type == "top_bottom":
|
| 24 |
+
# Increase height to create a rectangle
|
| 25 |
+
new_height = int(img.width * (4/3)) # Example: height = width * 4/3
|
| 26 |
+
newimg = Image.new(
|
| 27 |
+
'RGB',
|
| 28 |
+
(img.width, new_height),
|
| 29 |
+
(round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
|
| 30 |
+
)
|
| 31 |
+
padding_top = (new_height - img.height) // 2
|
| 32 |
+
newimg.paste(img, (0, padding_top)) # Center the image vertically
|
| 33 |
+
return newimg
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
elif padding_type == "left_right":
|
| 36 |
+
# Increase width to create a rectangle
|
| 37 |
+
new_width = int(img.height * (4/3)) # Example: width = height * 4/3
|
| 38 |
+
newimg = Image.new(
|
| 39 |
+
'RGB',
|
| 40 |
+
(new_width, img.height),
|
| 41 |
+
(round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
|
| 42 |
+
)
|
| 43 |
+
padding_left = (new_width - img.width) // 2
|
| 44 |
+
newimg.paste(img, (padding_left, 0)) # Center the image horizontally
|
| 45 |
+
return newimg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Function for cropping and filling the image
|
| 48 |
def fill_rectangle_cropper1(img):
|