Skip to main content
Glama

convert_to_grayscale

Convert color images to grayscale by processing file paths or base64 encoded data. This tool transforms RGB images to monochrome for simplified visual analysis or stylistic effects.

Instructions

将图片转换为灰度图

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
image_sourceYes图片源,可以是文件路径或base64编码的图片数据

Implementation Reference

  • The core handler function that loads the image, converts it to grayscale using PIL's image.convert('L'), processes the output, and returns JSON result.
    async def convert_to_grayscale(image_source: str) -> list[TextContent]: """ 将图片转换为灰度图 Args: image_source: 图片数据(base64编码)或文件路径 Returns: 灰度图片数据 """ try: # 验证参数 if not image_source: raise ValidationError("图片数据不能为空") # 加载图片 image = processor.load_image(image_source) # 转换为灰度图 grayscale_image = image.convert('L') # 输出处理后的图片 output_info = processor.output_image(grayscale_image, "grayscale") result = { "success": True, "message": "图片转换为灰度图成功", "data": { **output_info, "original_mode": image.mode, "new_mode": grayscale_image.mode, "size": image.size } } 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:444-456 (registration)
    MCP tool registration decorator (@mcp.tool()) with input schema via Annotated Field, wrapper function that safely calls the imported color_adjust.convert_to_grayscale handler.
    @mcp.tool() def convert_to_grayscale( image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")] ) -> str: """将图片转换为灰度图""" try: result = safe_run_async(color_convert_to_grayscale(image_source)) return result[0].text except Exception as e: return json.dumps({ "success": False, "error": f"转换为灰度图失败: {str(e)}" }, ensure_ascii=False, indent=2)
  • Tool schema definition in get_color_adjust_tools() list (though not directly used in main.py registration).
    Tool( name="convert_to_grayscale", description="将图片转换为灰度图", inputSchema={ "type": "object", "properties": { "image_source": { "type": "string", "description": "图片数据(base64编码)或文件路径" } }, "required": ["image_source"] } ),

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/duke0317/ps-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server