get_image_info
Extract image metadata and technical details from files or data inputs to analyze format, dimensions, and properties for informed processing decisions.
Instructions
获取图片文件信息
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get_image_info"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_path | No | 图片文件路径(与image_data二选一) | |
| image_data | No | 图片数据(Buffer或base64字符串,与image_path二选一) |
Implementation Reference
- src/index.ts:173-189 (registration)Registration of the 'get_image_info' tool in the ListTools handler, including its name, description, and input schema.
{ name: 'get_image_info', description: '获取图片文件信息', inputSchema: { type: 'object', properties: { image_path: { type: 'string', description: '图片文件路径(与image_data二选一)' }, image_data: { type: 'string', description: '图片数据(Buffer或base64字符串,与image_path二选一)' } } } }, - src/index.ts:264-276 (handler)MCP handler for 'get_image_info' tool in the CallToolRequestHandler switch statement. It calls converter.getImageInfo and formats the response text.
case 'get_image_info': { const { image_path, image_data } = args as { image_path?: string; image_data?: string }; const info = await this.converter.getImageInfo(image_path, image_data); const source = image_path ? `文件路径:${image_path}` : '上传文件'; return { content: [ { type: 'text', text: `图片信息:\n${source}\n格式:${info.format}\n尺寸:${info.width}x${info.height}\n文件大小:${info.size} bytes\n颜色通道:${info.channels}\n颜色空间:${info.space || '未知'}` } ] }; } - src/converter.ts:48-55 (schema)ImageInfo interface defining the structure of image information returned by getImageInfo.
export interface ImageInfo { format: string; width: number; height: number; channels: number; size: number; space?: string; } - src/converter.ts:295-330 (helper)Core implementation of getImageInfo in ImageConverter class using Sharp to extract metadata from image file or buffer.
async getImageInfo(imagePath?: string, imageData?: Buffer | string): Promise<ImageInfo> { try { let buffer: Buffer; let size: number; if (imageData) { if (typeof imageData === 'string') { const base64Data = imageData.includes(',') ? imageData.split(',')[1] : imageData; buffer = Buffer.from(base64Data, 'base64'); } else { buffer = imageData; } size = buffer.length; } else if (imagePath) { await fs.access(imagePath); const stats = await fs.stat(imagePath); buffer = await fs.readFile(imagePath); size = stats.size; } else { throw new Error('必须提供imagePath或imageData参数'); } const metadata = await sharp(buffer).metadata(); return { format: metadata.format || 'unknown', width: metadata.width || 0, height: metadata.height || 0, channels: metadata.channels || 0, size: size, space: metadata.space }; } catch (error) { throw new Error(`无法获取图片信息: ${error instanceof Error ? error.message : String(error)}`); } }