itsyogesh commited on
Commit
42d6656
·
verified ·
1 Parent(s): a7f9f53

Updated crop function to return alpha image

Browse files
Files changed (1) hide show
  1. app.py +53 -1
app.py CHANGED
@@ -91,6 +91,58 @@ def build_model(hypar,device):
91
  return net
92
 
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  def crop_signature(original_image_path, mask, padding=32):
95
  """
96
  Crop the signature from the original image using the provided mask.
@@ -133,7 +185,7 @@ def crop_signature(original_image_path, mask, padding=32):
133
 
134
  # If no contours are found, return the original image
135
  return original_image
136
-
137
 
138
  def smooth_and_denoise(mask):
139
  """
 
91
  return net
92
 
93
 
94
+ def crop_signature(original_image_path, mask, padding=32):
95
+ """
96
+ Crop the signature from the original image using the provided mask and create a transparent image.
97
+ :param original_image_path: The file path of the original image.
98
+ :param mask: The binary mask of the signature.
99
+ :param padding: Padding to add around the bounding box of the signature.
100
+ :return: Cropped transparent image containing the signature.
101
+ """
102
+ # Convert the mask to a binary image
103
+ _, binary_mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
104
+
105
+ # Find contours from the binary mask
106
+ contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
107
+
108
+ # Open the original image
109
+ original_image = Image.open(original_image_path).convert("RGB")
110
+
111
+ # If contours are found, proceed to crop
112
+ if contours:
113
+ # Find the combined bounding box of all contours
114
+ min_x, min_y = original_image.width, original_image.height
115
+ max_x = max_y = 0
116
+ for contour in contours:
117
+ x, y, w, h = cv2.boundingRect(contour)
118
+ min_x, min_y = min(min_x, x), min(min_y, y)
119
+ max_x, max_y = max(max_x, x + w), max(max_y, y + h)
120
+
121
+ # Add padding to the bounding box
122
+ x_padded = max(min_x - padding, 0)
123
+ y_padded = max(min_y - padding, 0)
124
+ w_padded = min(max_x + padding, original_image.width) - x_padded
125
+ h_padded = min(max_y + padding, original_image.height) - y_padded
126
+
127
+ # Crop the original image using the combined bounding box with padding
128
+ cropped_image = original_image.crop((x_padded, y_padded, x_padded + w_padded, y_padded + h_padded))
129
+
130
+ # Create a new image for the mask with the same size as the cropped image and make it transparent
131
+ mask_image = Image.new("L", cropped_image.size, 0)
132
+ mask_array = np.array(mask, dtype=np.uint8) # Convert the mask to an array
133
+ # Crop the mask array to the same bounding box
134
+ cropped_mask_array = mask_array[y_padded:y_padded+h_padded, x_padded:x_padded+w_padded]
135
+ mask_image.putdata(cropped_mask_array.flatten())
136
+
137
+ # Convert mask image to alpha channel and combine it with the cropped image
138
+ cropped_image.putalpha(mask_image)
139
+
140
+ return cropped_image
141
+
142
+ # If no contours are found, return the original image
143
+ return original_image.convert("RGBA")
144
+
145
+ '''
146
  def crop_signature(original_image_path, mask, padding=32):
147
  """
148
  Crop the signature from the original image using the provided mask.
 
185
 
186
  # If no contours are found, return the original image
187
  return original_image
188
+ '''
189
 
190
  def smooth_and_denoise(mask):
191
  """