base64_encode_image
Convert images to Base64 encoded strings for integration into web applications or embedding in HTML/CSS using the specified image file path.
Instructions
将图片转换为Base64编码
Args:
image_path: 图片文件路径
Returns:
Base64编码结果
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_path | Yes |
Implementation Reference
- base64_server.py:69-97 (handler)The handler function decorated with @mcp.tool() that implements the base64_encode_image tool. It reads an image file, encodes it to base64, determines the MIME type using PIL, and returns a preview of the base64 string and data URL.@mcp.tool() def base64_encode_image(image_path: str) -> str: """将图片转换为Base64编码 Args: image_path: 图片文件路径 Returns: Base64编码结果 """ try: if not os.path.exists(image_path): return f"错误: 文件 '{image_path}' 不存在" with open(image_path, "rb") as image_file: encoded = base64.b64encode(image_file.read()).decode("utf-8") # 获取MIME类型 img = PILImage.open(image_path) mime_type = f"image/{img.format.lower()}" # 返回可在HTML中使用的Data URL格式 data_url = f"data:{mime_type};base64,{encoded}" encoded_preview = f"图片Base64编码结果 (前100字符): {encoded[:100]}..." data_url_preview = f"完整Data URL (前100字符): {data_url[:100]}..." return f"{encoded_preview}\n\n{data_url_preview}" except Exception as e: return f"图片编码失败: {str(e)}"