get_image_info
Extract and display essential details from images, including file path or base64 data, using a specific tool within the Image Processing MCP Server.
Instructions
获取图片基本信息
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_source | Yes | 图片源,可以是文件路径或base64编码的图片数据 |
Implementation Reference
- tools/basic.py:214-275 (handler)Main handler function for the get_image_info tool. Loads the image from source, retrieves basic info using ImageProcessor, computes additional metrics like data size, aspect ratio, total pixels, and file info if applicable, then returns a JSON-formatted response.async def get_image_info(image_source: str) -> list[TextContent]: """ 获取图片信息 Args: image_source: 图片源(文件路径或base64编码数据) Returns: 图片信息响应 """ try: # 验证参数 if not image_source: raise ValidationError("图片源不能为空") # 加载图片 image = processor.load_image(image_source) # 获取详细信息 info = processor.get_image_info(image) # 计算图片大小(字节) import io buffer = io.BytesIO() image.save(buffer, format=image.format or 'PNG') data_size = len(buffer.getvalue()) # 扩展信息 extended_info = { **info, "data_size": data_size, "aspect_ratio": round(info['width'] / info['height'], 2), "total_pixels": info['width'] * info['height'] } # 如果是文件路径,添加文件信息 if not image_source.startswith('data:image') and os.path.exists(image_source): extended_info["file_path"] = image_source extended_info["file_size_bytes"] = os.path.getsize(image_source) result = { "success": True, "message": "获取图片信息成功", "data": extended_info } 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))]
- tools/basic.py:56-69 (schema)Tool schema definition including input schema for image_data (base64 string) in the get_basic_tools() function.Tool( name="get_image_info", description="获取图片基本信息", inputSchema={ "type": "object", "properties": { "image_data": { "type": "string", "description": "图片数据(base64编码)" } }, "required": ["image_data"] } ),
- main.py:138-151 (registration)MCP tool registration using @mcp.tool() decorator. Wraps the basic_get_image_info async handler and provides Pydantic schema via Annotated Field.@mcp.tool() def get_image_info( image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")] ) -> str: """获取图片基本信息""" try: result = safe_run_async(basic_get_image_info(image_source)) return result[0].text except Exception as e: return json.dumps({ "success": False, "error": f"获取图片信息失败: {str(e)}" }, ensure_ascii=False, indent=2)
- utils/image_processor.py:129-146 (helper)Core helper method in ImageProcessor class that extracts basic image properties (size, mode, format, width, height) from a PIL Image object. Called by the main handler.def get_image_info(self, image: Image.Image) -> dict: """ 获取图片信息 Args: image: PIL Image对象 Returns: 包含图片信息的字典 """ return { 'size': image.size, 'mode': image.mode, 'format': image.format, 'width': image.width, 'height': image.height }