rotate_image
Rotate images by specified angles with options to expand canvas or crop, and set background fill colors for image processing workflows.
Instructions
旋转图片
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_source | Yes | 图片源,可以是文件路径或base64编码的图片数据 | |
| angle | Yes | 旋转角度(度),正值为顺时针,负值为逆时针 | |
| expand | No | 是否扩展画布以容纳旋转后的图片,False会裁剪 | |
| fill_color | No | 填充颜色,十六进制格式如 #FFFFFF(白色) | #FFFFFF |
Implementation Reference
- tools/transform.py:286-351 (handler)Core handler function that loads the image, validates parameters, rotates it using PIL.Image.rotate, processes output, and returns JSON result.async def rotate_image(image_data: str, angle: float, expand: bool = True, fill_color: str = "#FFFFFF") -> list[TextContent]: """ 旋转图片 Args: image_data: 图片数据(base64编码) angle: 旋转角度(度,正值为顺时针) expand: 是否扩展画布 fill_color: 填充颜色 Returns: 旋转后的图片数据 """ try: # 验证参数 if not image_data: raise ValidationError("图片数据不能为空") if not validate_color_hex(fill_color): raise ValidationError(f"无效的颜色格式: {fill_color}") # 加载图片 image = processor.load_image(image_data) original_size = image.size # 转换颜色格式 fill_rgb = tuple(int(fill_color[i:i+2], 16) for i in (1, 3, 5)) # 旋转图片 rotated_image = image.rotate( -angle, # PIL中负值为顺时针 expand=expand, fillcolor=fill_rgb ) # 输出旋转后的图片 output_info = processor.output_image(rotated_image, f"rotate_{int(angle)}deg") result = { "success": True, "message": f"图片旋转成功: {angle}度", "data": { **output_info, "original_size": original_size, "rotated_size": rotated_image.size, "angle": angle, "expand": expand, "fill_color": fill_color } } 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:206-222 (registration)MCP tool registration for 'rotate_image' using @mcp.tool() decorator. This wrapper function calls the core handler from tools.transform and handles errors.@mcp.tool() def rotate_image( image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")], angle: Annotated[float, Field(description="旋转角度(度),正值为顺时针,负值为逆时针")], expand: Annotated[bool, Field(description="是否扩展画布以容纳旋转后的图片,False会裁剪", default=True)], fill_color: Annotated[str, Field(description="填充颜色,十六进制格式如 #FFFFFF(白色)", default="#FFFFFF")] ) -> str: """旋转图片""" try: result = safe_run_async(transform_rotate_image(image_source, angle, expand, fill_color)) return result[0].text except Exception as e: return json.dumps({ "success": False, "error": f"旋转图片失败: {str(e)}" }, ensure_ascii=False, indent=2)
- tools/transform.py:80-107 (schema)Explicit input schema definition for the rotate_image tool, returned by get_transform_tools() function.Tool( name="rotate_image", description="旋转图片", inputSchema={ "type": "object", "properties": { "image_data": { "type": "string", "description": "图片数据(base64编码)" }, "angle": { "type": "number", "description": "旋转角度(度,正值为顺时针)" }, "expand": { "type": "boolean", "description": "是否扩展画布以容纳旋转后的图片", "default": True }, "fill_color": { "type": "string", "description": "填充颜色(十六进制格式,如#FFFFFF)", "default": "#FFFFFF" } }, "required": ["image_data", "angle"] } ),