New crop functionality
Browse files
app.py
CHANGED
|
@@ -73,7 +73,7 @@ def build_model(hypar,device):
|
|
| 73 |
def crop_signature(original_image_path, mask, padding=32):
|
| 74 |
"""
|
| 75 |
Crop the signature from the original image using the provided mask.
|
| 76 |
-
|
| 77 |
:param original_image_path: The file path of the original image.
|
| 78 |
:param mask: The binary mask of the signature.
|
| 79 |
:param padding: Padding to add around the bounding box of the signature.
|
|
@@ -82,34 +82,34 @@ def crop_signature(original_image_path, mask, padding=32):
|
|
| 82 |
# Convert the mask to a binary image
|
| 83 |
_, binary_mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
dilated_mask = cv2.dilate(binary_mask, kernel, iterations=1)
|
| 88 |
|
| 89 |
-
# Find contours from the binary (possibly dilated) mask
|
| 90 |
-
contours, _ = cv2.findContours(dilated_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 91 |
-
|
| 92 |
# Open the original image
|
| 93 |
original_image = Image.open(original_image_path).convert("RGB")
|
| 94 |
-
|
| 95 |
# If contours are found, proceed to crop
|
| 96 |
if contours:
|
| 97 |
-
#
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# Add padding to the bounding box
|
| 104 |
-
x_padded = max(
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
# Crop the original image and mask using the bounding box with padding
|
| 109 |
-
cropped_image = original_image.crop((x_padded, y_adjusted, x_padded + w_padded, y_adjusted + h_padded))
|
| 110 |
-
|
| 111 |
return cropped_image
|
| 112 |
-
|
| 113 |
# If no contours are found, return the original image
|
| 114 |
return original_image
|
| 115 |
|
|
|
|
| 73 |
def crop_signature(original_image_path, mask, padding=32):
|
| 74 |
"""
|
| 75 |
Crop the signature from the original image using the provided mask.
|
| 76 |
+
|
| 77 |
:param original_image_path: The file path of the original image.
|
| 78 |
:param mask: The binary mask of the signature.
|
| 79 |
:param padding: Padding to add around the bounding box of the signature.
|
|
|
|
| 82 |
# Convert the mask to a binary image
|
| 83 |
_, binary_mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
|
| 84 |
|
| 85 |
+
# Find contours from the binary mask
|
| 86 |
+
contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
# Open the original image
|
| 89 |
original_image = Image.open(original_image_path).convert("RGB")
|
| 90 |
+
|
| 91 |
# If contours are found, proceed to crop
|
| 92 |
if contours:
|
| 93 |
+
# Find the combined bounding box of all contours
|
| 94 |
+
min_x, min_y = original_image.width, original_image.height
|
| 95 |
+
max_x = max_y = 0
|
| 96 |
|
| 97 |
+
for contour in contours:
|
| 98 |
+
x, y, w, h = cv2.boundingRect(contour)
|
| 99 |
+
min_x, min_y = min(min_x, x), min(min_y, y)
|
| 100 |
+
max_x, max_y = max(max_x, x + w), max(max_y, y + h)
|
| 101 |
|
| 102 |
# Add padding to the bounding box
|
| 103 |
+
x_padded = max(min_x - padding, 0)
|
| 104 |
+
y_padded = max(min_y - padding, 0)
|
| 105 |
+
w_padded = min(max_x + padding, original_image.width) - x_padded
|
| 106 |
+
h_padded = min(max_y + padding, original_image.height) - y_padded
|
| 107 |
+
|
| 108 |
+
# Crop the original image using the combined bounding box with padding
|
| 109 |
+
cropped_image = original_image.crop((x_padded, y_padded, x_padded + w_padded, y_padded + h_padded))
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
return cropped_image
|
| 112 |
+
|
| 113 |
# If no contours are found, return the original image
|
| 114 |
return original_image
|
| 115 |
|