Skip to main content
Glama
duke0317

Image Processing MCP Server

by duke0317

adjust_gamma

Adjust image brightness and contrast by modifying gamma values to correct exposure or enhance visual details in digital images.

Instructions

调整图片伽马值

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
image_sourceYes图片源,可以是文件路径或base64编码的图片数据
gammaYes伽马值,1.0为原始,>1.0变亮,<1.0变暗

Implementation Reference

  • Core handler function implementing the gamma adjustment logic. Validates inputs, loads image using ImageProcessor, applies gamma correction via lookup table and PIL's point() method for RGB/L/ other modes, processes output, and returns JSON result.
    async def adjust_gamma(image_source: str, gamma: float) -> list[TextContent]:
        """
        调整图片伽马值
        
        Args:
            image_source: 图片数据(base64编码)或文件路径
            gamma: 伽马值(0.1-3.0)
            
        Returns:
            调整后的图片数据
        """
        try:
            # 验证参数
            if not image_source:
                raise ValidationError("图片数据不能为空")
            
            if not validate_numeric_range(gamma, 0.1, 3.0):
                raise ValidationError(f"伽马值必须在0.1-3.0范围内: {gamma}")
            
            # 加载图片
            image = processor.load_image(image_source)
            
            # 创建伽马校正查找表
            gamma_table = [int(((i / 255.0) ** (1.0 / gamma)) * 255) for i in range(256)]
            
            # 应用伽马校正
            if image.mode == 'RGB':
                # 对RGB图片分别处理每个通道
                r, g, b = image.split()
                r = r.point(gamma_table)
                g = g.point(gamma_table)
                b = b.point(gamma_table)
                gamma_image = Image.merge('RGB', (r, g, b))
            elif image.mode == 'L':
                # 对灰度图直接处理
                gamma_image = image.point(gamma_table)
            else:
                # 其他模式先转换为RGB
                rgb_image = image.convert('RGB')
                r, g, b = rgb_image.split()
                r = r.point(gamma_table)
                g = g.point(gamma_table)
                b = b.point(gamma_table)
                gamma_image = Image.merge('RGB', (r, g, b))
            
            # 输出处理后的图片
            output_info = processor.output_image(gamma_image, "gamma")
            
            result = {
                "success": True,
                "message": f"伽马调整成功: 伽马值 {gamma}",
                "data": {
                    **output_info,
                    "gamma_value": gamma,
                    "original_mode": 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:458-472 (registration)
    Tool registration via @mcp.tool() decorator in FastMCP server. Defines input schema using Pydantic Annotated fields and provides a synchronous wrapper that calls the async handler from color_adjust module using safe_run_async.
    @mcp.tool()
    def adjust_gamma(
        image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")],
        gamma: Annotated[float, Field(description="伽马值,1.0为原始,>1.0变亮,<1.0变暗", gt=0)]
    ) -> str:
        """调整图片伽马值"""
        try:
            result = safe_run_async(color_adjust_gamma(image_source, gamma))
            return result[0].text
        except Exception as e:
            return json.dumps({
                "success": False,
                "error": f"调整伽马值失败: {str(e)}"
            }, ensure_ascii=False, indent=2)
  • Explicit Tool schema definition for adjust_gamma in get_color_adjust_tools(), matching the registered input parameters (though not directly used in MCP registration).
    Tool(
        name="adjust_gamma",
        description="调整图片伽马值",
        inputSchema={
            "type": "object",
            "properties": {
                "image_source": {
                    "type": "string",
                    "description": "图片数据(base64编码)或文件路径"
                },
                "gamma": {
                    "type": "number",
                    "description": "伽马值(0.1-3.0,1.0为原始值)",
                    "minimum": 0.1,
                    "maximum": 3.0
                }
            },
            "required": ["image_source", "gamma"]
        }
    ),

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