Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -73,7 +73,22 @@ def find_optimal_crop(image, mask, target_aspect_ratio):
|
|
| 73 |
image_np = np.array(image)
|
| 74 |
h, w = mask.shape
|
| 75 |
|
| 76 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
if target_aspect_ratio > w / h:
|
| 78 |
# Target is wider than original
|
| 79 |
target_h = int(w / target_aspect_ratio)
|
|
@@ -83,24 +98,39 @@ def find_optimal_crop(image, mask, target_aspect_ratio):
|
|
| 83 |
target_h = h
|
| 84 |
target_w = int(h * target_aspect_ratio)
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
return
|
| 104 |
|
| 105 |
def smart_crop(input_image, target_aspect_ratio, point_x=None, point_y=None):
|
| 106 |
"""
|
|
|
|
| 73 |
image_np = np.array(image)
|
| 74 |
h, w = mask.shape
|
| 75 |
|
| 76 |
+
# Find the bounding box of the important content
|
| 77 |
+
# First, find where the mask is non-zero (important content)
|
| 78 |
+
y_indices, x_indices = np.where(mask > 0)
|
| 79 |
+
|
| 80 |
+
if len(y_indices) == 0 or len(x_indices) == 0:
|
| 81 |
+
# Fallback if no mask is found
|
| 82 |
+
content_box = (0, 0, w, h)
|
| 83 |
+
else:
|
| 84 |
+
# Get the bounding box of important content
|
| 85 |
+
min_x, max_x = np.min(x_indices), np.max(x_indices)
|
| 86 |
+
min_y, max_y = np.min(y_indices), np.max(y_indices)
|
| 87 |
+
content_width = max_x - min_x + 1
|
| 88 |
+
content_height = max_y - min_y + 1
|
| 89 |
+
content_box = (min_x, min_y, content_width, content_height)
|
| 90 |
+
|
| 91 |
+
# Calculate target dimensions based on the original image
|
| 92 |
if target_aspect_ratio > w / h:
|
| 93 |
# Target is wider than original
|
| 94 |
target_h = int(w / target_aspect_ratio)
|
|
|
|
| 98 |
target_h = h
|
| 99 |
target_w = int(h * target_aspect_ratio)
|
| 100 |
|
| 101 |
+
# Calculate the center of the important content
|
| 102 |
+
content_center_x = content_box[0] + content_box[2] // 2
|
| 103 |
+
content_center_y = content_box[1] + content_box[3] // 2
|
| 104 |
|
| 105 |
+
# Try to center the crop on the important content
|
| 106 |
+
x = max(0, min(content_center_x - target_w // 2, w - target_w))
|
| 107 |
+
y = max(0, min(content_center_y - target_h // 2, h - target_h))
|
| 108 |
|
| 109 |
+
# Check if the important content fits within this crop
|
| 110 |
+
min_x, min_y, content_width, content_height = content_box
|
| 111 |
+
max_x = min_x + content_width
|
| 112 |
+
max_y = min_y + content_height
|
| 113 |
+
|
| 114 |
+
# If the content doesn't fit in the crop, adjust the crop
|
| 115 |
+
if target_w >= content_width and target_h >= content_height:
|
| 116 |
+
# If the crop is large enough to include all content, center it
|
| 117 |
+
x = max(0, min(content_center_x - target_w // 2, w - target_w))
|
| 118 |
+
y = max(0, min(content_center_y - target_h // 2, h - target_h))
|
| 119 |
+
else:
|
| 120 |
+
# If crop isn't large enough for all content, maximize visible content
|
| 121 |
+
# and prioritize centering the crop on the content
|
| 122 |
+
x = max(0, min(min_x, w - target_w))
|
| 123 |
+
y = max(0, min(min_y, h - target_h))
|
| 124 |
+
|
| 125 |
+
# If we still can't fit width, center the crop horizontally
|
| 126 |
+
if content_width > target_w:
|
| 127 |
+
x = max(0, min(content_center_x - target_w // 2, w - target_w))
|
| 128 |
+
|
| 129 |
+
# If we still can't fit height, center the crop vertically
|
| 130 |
+
if content_height > target_h:
|
| 131 |
+
y = max(0, min(content_center_y - target_h // 2, h - target_h))
|
| 132 |
|
| 133 |
+
return (x, y, x + target_w, y + target_h)
|
| 134 |
|
| 135 |
def smart_crop(input_image, target_aspect_ratio, point_x=None, point_y=None):
|
| 136 |
"""
|