79682485

Date: 2025-06-27 21:13:53
Score: 1
Natty:
Report link

# Convert image to grayscale again for contour detection

gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)

# Apply threshold to get binary image

_, thresh = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)

# Find contours

contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Create a black mask and draw the contours

mask = np.zeros_like(image_np)

cv2.drawContours(mask, contours, -1, (255, 255, 255), thickness=cv2.FILLED)

# Apply mask to extract the figure

result = cv2.bitwise_and(image_np, mask)

# Convert white background to transparent

rgba_image = cv2.cvtColor(result, cv2.COLOR_RGB2RGBA)

rgba_image[np.all(mask == 0, axis=-1)] = [0, 0, 0, 0]

# Save the final image with transparency

output_image = Image.fromarray(rgba_image)

output_path = "/mnt/data/mascara_isolado_transparente.png"

output_image.save(output_path)

output_path

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: Iagoo BF