background_removal
Remove image backgrounds automatically to isolate subjects. Input an image path and receive a cleaned image file, enhancing focus on the main content for editing or design purposes.
Instructions
Remove the background of an image automatically.
Args:
image_path: File path of the original image
ctx: MCP context
Returns:
Dict: Dictionary containing the file path of the image with the background removed
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_path | Yes |
Implementation Reference
- The handler function that implements the background removal logic: reads input image, encodes to base64, sends to Bedrock API with BACKGROUND_REMOVAL task, saves output image, returns path and message.async def background_removal( image_path: str, output_path: str = None, ctx: Context = None, ) -> Dict[str, Any]: """ Remove the background of an image automatically. Args: image_path: File path of the original image, Please use the actual local file path where the image is stored. output_path: Absolute path to save the image ctx: MCP context Returns: Dict: Dictionary containing the file path of the image with the background removed """ try: # Read image file and encode to base64 with open(image_path, "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') body = json.dumps({ "taskType": "BACKGROUND_REMOVAL", "backgroundRemovalParams": { "image": input_image, } }) # Generate image image_bytes = generate_image(body) # Save image image_info = save_image(image_bytes, output_path=output_path) # Generate result result = { "image_path": image_info["image_path"], "message": f"Background removed successfully. Saved location: {image_info['image_path']}" } return result except Exception as e: raise McpError(f"Error occurred while removing background: {str(e)}")
- aws_nova_canvas_mcp/server.py:37-37 (registration)Registers the background_removal tool with the FastMCP server instance.mcp.add_tool(background_removal)