add_border
Add customizable borders to images using width, color, style, and corner radius options. Supports various formats like PNG, JPEG, and WEBP for tailored image enhancements.
Instructions
为图片添加边框效果
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| border_color | No | 边框颜色,十六进制格式如 #000000(黑色) | #000000 |
| border_style | No | 边框样式:solid(实线)、dashed(虚线)、dotted(点线) | solid |
| border_width | No | 边框宽度(像素) | |
| corner_radius | No | 圆角半径(像素),0为直角 | |
| image_source | Yes | 图片源,可以是文件路径或base64编码的图片数据 | |
| output_format | No | 输出格式:PNG、JPEG、WEBP 等 | PNG |
Implementation Reference
- tools/effects.py:304-416 (handler)Core handler function that implements the add_border tool logic using PIL to add solid, rounded, or shadow borders to images, with parameter validation and base64 output.async def add_border(arguments: Dict[str, Any]) -> List[TextContent]: """ 为图片添加边框效果 Args: arguments: 包含图片源和边框参数的字典 Returns: List[TextContent]: 处理结果 """ try: # 参数验证 image_source = arguments.get("image_source") ensure_valid_image_source(image_source) border_width = arguments.get("border_width", 10) border_color = arguments.get("border_color", "#000000") border_style = arguments.get("border_style", "solid") corner_radius = arguments.get("corner_radius", 10) output_format = arguments.get("output_format", DEFAULT_IMAGE_FORMAT) # 验证参数 validate_numeric_range(border_width, 1, 100, "border_width") validate_color_hex(border_color) validate_numeric_range(corner_radius, 0, 50, "corner_radius") # 加载图片 processor = ImageProcessor() image = processor.load_image(image_source) # 创建带边框的新图片 new_width = image.width + 2 * border_width new_height = image.height + 2 * border_width if border_style == "rounded": # 圆角边框 bordered_image = Image.new("RGBA", (new_width, new_height), border_color) # 创建圆角遮罩 mask = Image.new("L", (new_width, new_height), 0) draw = ImageDraw.Draw(mask) draw.rounded_rectangle( [0, 0, new_width, new_height], radius=corner_radius, fill=255 ) # 应用遮罩 bordered_image.putalpha(mask) # 粘贴原图片 if image.mode != "RGBA": image = image.convert("RGBA") bordered_image.paste(image, (border_width, border_width), image) elif border_style == "shadow": # 阴影边框 bordered_image = Image.new("RGBA", (new_width + 10, new_height + 10), (0, 0, 0, 0)) # 创建阴影 shadow = Image.new("RGBA", (new_width, new_height), border_color + "80") shadow = shadow.filter(ImageFilter.GaussianBlur(radius=5)) bordered_image.paste(shadow, (10, 10), shadow) # 粘贴原图片 if image.mode != "RGBA": image = image.convert("RGBA") bordered_image.paste(image, (border_width, border_width), image) else: # 实心边框 bordered_image = Image.new("RGB", (new_width, new_height), border_color) bordered_image.paste(image, (border_width, border_width)) # 转换为base64 output_info = processor.output_image(bordered_image, "border", output_format) return [TextContent( type="text", text=json.dumps({ "success": True, "message": f"成功添加{border_style}边框效果", "data": { **output_info, "metadata": { "original_size": f"{image.width}x{image.height}", "new_size": f"{bordered_image.width}x{bordered_image.height}", "border_width": border_width, "border_color": border_color, "border_style": border_style, "format": output_format } } }, ensure_ascii=False) )] except ValidationError as e: return [TextContent( type="text", text=json.dumps({ "success": False, "error": f"参数验证失败: {str(e)}" }, ensure_ascii=False) )] except Exception as e: return [TextContent( type="text", text=json.dumps({ "success": False, "error": f"添加边框失败: {str(e)}" }, ensure_ascii=False) )]
- main.py:490-515 (registration)MCP tool registration for 'add_border' using @mcp.tool() decorator. Defines input schema via Annotated parameters and wraps the core handler from tools/effects.@mcp.tool() def add_border( image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")], border_width: Annotated[int, Field(description="边框宽度(像素)", ge=1, default=10)], border_color: Annotated[str, Field(description="边框颜色,十六进制格式如 #000000(黑色)", default="#000000")], border_style: Annotated[str, Field(description="边框样式:solid(实线)、dashed(虚线)、dotted(点线)", default="solid")], corner_radius: Annotated[int, Field(description="圆角半径(像素),0为直角", ge=0, default=10)], output_format: Annotated[str, Field(description="输出格式:PNG、JPEG、WEBP 等", default="PNG")] ) -> str: """为图片添加边框效果""" try: arguments = { "image_source": image_source, "border_width": border_width, "border_color": border_color, "border_style": border_style, "corner_radius": corner_radius, "output_format": output_format } result = safe_run_async(effects_add_border(arguments)) return result[0].text except Exception as e: return json.dumps({ "success": False, "error": f"添加边框失败: {str(e)}" }, ensure_ascii=False, indent=2)
- tools/effects.py:32-76 (schema)Input schema definition for add_border tool in get_effect_tools(), matching the parameters used in handler and registration (though registration uses pydantic annotations).Tool( name="add_border", description="为图片添加边框效果", inputSchema={ "type": "object", "properties": { "image_source": { "type": "string", "description": "图片源(文件路径或base64编码)" }, "border_width": { "type": "integer", "description": "边框宽度(像素)", "minimum": 1, "maximum": 100, "default": 10 }, "border_color": { "type": "string", "description": "边框颜色(十六进制格式,如#FF0000)", "default": "#000000" }, "border_style": { "type": "string", "description": "边框样式", "enum": ["solid", "rounded", "shadow"], "default": "solid" }, "corner_radius": { "type": "integer", "description": "圆角半径(仅当border_style为rounded时有效)", "minimum": 0, "maximum": 50, "default": 10 }, "output_format": { "type": "string", "description": "输出格式", "enum": ["PNG", "JPEG", "WEBP"], "default": "PNG" } }, "required": ["image_source"] } ),