base64_decode_image
Decode Base64 encoded strings into image files. Specify the output path and MIME type to convert encoded data into a usable image format.
Instructions
将Base64编码解码为图片
Args:
encoded: Base64编码的字符串
output_path: 输出图片的路径
mime_type: 图片的MIME类型 (默认为image/png)
Returns:
解码结果
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| encoded | Yes | ||
| mime_type | No | image/png | |
| output_path | Yes |
Implementation Reference
- base64_server.py:99-139 (handler)The handler function for the base64_decode_image MCP tool. It decodes base64 strings (including data URLs) to image files, creates directories if needed, and returns success or error message.@mcp.tool() def base64_decode_image( encoded: str, output_path: str, mime_type: str = "image/png" ) -> str: """将Base64编码解码为图片 Args: encoded: Base64编码的字符串 output_path: 输出图片的路径 mime_type: 图片的MIME类型 (默认为image/png) Returns: 解码结果 """ try: # 清理输入,移除可能的前缀和空白 encoded = encoded.strip() # 如果输入是Data URL格式,提取实际的Base64部分 if encoded.startswith("data:"): # 分离MIME类型和Base64内容 header, encoded = encoded.split(",", 1) if ";base64" not in header: return "错误: 输入不是有效的Base64编码的Data URL" # 提取MIME类型但不使用,因为我们使用参数中的mime_type # 解码Base64 image_data = base64.b64decode(encoded) # 确保输出目录存在 output_dir = os.path.dirname(output_path) if output_dir and not os.path.exists(output_dir): os.makedirs(output_dir) # 保存图片 with open(output_path, "wb") as image_file: image_file.write(image_data) return f"图片已成功解码并保存到 {output_path}" except Exception as e: return f"图片解码失败: {str(e)}"