apply_invert
Invert colors in images to create negative effects or correct color issues. Process images by providing file paths or base64 data for color reversal.
Instructions
应用反色滤镜
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_source | Yes | 图片源,可以是文件路径或base64编码的图片数据 |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- tools/filters.py:660-708 (handler)Core handler function that loads the image, applies color inversion using PIL's ImageOps.invert, processes output via ImageProcessor, and returns JSON result.
async def apply_invert(image_data: str) -> list[TextContent]: """ 应用反色滤镜 Args: image_data: 图片数据(base64编码) Returns: 应用滤镜后的图片数据 """ try: # 验证参数 if not image_data: raise ValidationError("图片数据不能为空") # 加载图片 image = processor.load_image(image_data) # 应用反色滤镜 inverted_image = ImageOps.invert(image.convert('RGB')) # 输出处理后的图片 output_info = processor.output_image(inverted_image, "invert") result = { "success": True, "message": "反色滤镜应用成功", "data": { **output_info, "filter_type": "invert", "size": image.size } } return [TextContent(type="text", text=json.dumps(result, ensure_ascii=False))] except ValidationError as e: error_result = { "success": False, "error": f"参数验证失败: {str(e)}" } return [TextContent(type="text", text=json.dumps(error_result, ensure_ascii=False))] except Exception as e: error_result = { "success": False, "error": f"反色滤镜应用失败: {str(e)}" } return [TextContent(type="text", text=json.dumps(error_result, ensure_ascii=False))] - main.py:368-381 (registration)Registers the apply_invert tool with the MCP server using the @mcp.tool decorator. Defines the input schema via Annotated Field and wraps the call to the core filters.apply_invert handler.
@mcp.tool() def apply_invert( image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")] ) -> str: """应用反色滤镜""" try: result = safe_run_async(filters_apply_invert(image_source)) return result[0].text except Exception as e: return json.dumps({ "success": False, "error": f"应用反色效果失败: {str(e)}" }, ensure_ascii=False, indent=2) - tools/filters.py:163-176 (schema)Tool schema definition for apply_invert, including input schema for image_data parameter, as part of get_filter_tools().
Tool( name="apply_invert", description="应用反色滤镜", inputSchema={ "type": "object", "properties": { "image_data": { "type": "string", "description": "图片数据(base64编码)" } }, "required": ["image_data"] } )