get_image_info
Extract basic image metadata such as dimensions, format, and size from file paths or base64 encoded data to analyze and verify image properties.
Instructions
获取图片基本信息
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_source | Yes | 图片源,可以是文件路径或base64编码的图片数据 |
Implementation Reference
- tools/basic.py:214-274 (handler)Core asynchronous handler function that implements the get_image_info tool logic: validates input, loads image using ImageProcessor, retrieves basic info, computes extended metrics (data size, aspect ratio, pixels, file info), and returns formatted JSON 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))]
- main.py:138-151 (registration)FastMCP tool registration decorator (@mcp.tool()) that defines the public tool interface, calls the core handler via safe_run_async, and handles errors by returning JSON.@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)
- tools/basic.py:56-69 (schema)Tool schema definition in get_basic_tools() function, specifying input requirements (image_data as base64 string) and description for the get_image_info tool.Tool( name="get_image_info", description="获取图片基本信息", inputSchema={ "type": "object", "properties": { "image_data": { "type": "string", "description": "图片数据(base64编码)" } }, "required": ["image_data"] } ),
- utils/image_processor.py:129-146 (helper)Supporting utility method in ImageProcessor class that extracts basic PIL Image properties (size, mode, format, dimensions) used 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 }