I want to make a mask that covers more ground but the spread only applies on the…
- User
I want to make a mask that covers more ground but the spread only applies on the rightside of the object
def create_mask(image_path): """ Creates a mask for the given image with a stroke effect while maintaining the original image size.
codeArgs: image_path (str): Path to the input image. Returns: str: Path to the created mask image. """ try: b64_image = detect_clothes(image_path) im = Image.open(BytesIO(base64.b64decode(b64_image[len("data:image/png;base64,"):]))) im = im.convert("RGBA") # Separate the image into RGB and alpha channels r, g, b, alpha = im.split() # Create a mask of the non-transparent pixels mask = alpha.point(lambda p: 255 if p > 0 else 0).convert("L") # Create a larger canvas for the stroke to prevent dimension changes stroke_size = 10 # Stroke size in pixels larger_canvas_size = (im.width + 2 * stroke_size, im.height + 2 * stroke_size) stroke = Image.new("L", larger_canvas_size, 0) # Larger canvas for the stroke stroke.paste(mask, (stroke_size, stroke_size)) # Paste mask in the center of the larger canvas # Expand the mask to create the stroke effect stroke = ImageOps.expand(stroke, border=stroke_size, fill=255) # Crop the stroke back to the original image size stroke = stroke.crop((stroke_size, stroke_size, stroke_size + im.width, stroke_size + im.height)) # Create the final image combining the stroke with the original mask stroke_image = Image.new("RGBA", im.size, (0, 0, 0, 0)) black_image = Image.new("RGBA", im.size, (0, 0, 0, 255)) black_image.putalpha(alpha) stroke_image.paste(black_image, (0, 0), mask=mask) stroke_image.paste((0, 0, 0, 255), (0, 0), mask=stroke) # Apply the stroke around the shape # Save the final image mask_path = os.path.splitext(image_path)[0] + "_mask.png" stroke_image.save(mask_path) print(f"Mask created at: {mask_path}") return mask_path except Exception as e: print(f"Error in creating mask: {e}") sys.exit(1) - Assistant
- User
Error in creating mask: Incorrect padding
- Assistant
- User
Make keep the original image size
- Assistant