Skip to main content
Glama
duke0317

Image Processing MCP Server

by duke0317

create_silhouette

Convert images into silhouette effects by processing image sources with customizable color, background, threshold, and output format settings.

Instructions

创建图片的剪影效果

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
image_sourceYes图片源,可以是文件路径或base64编码的图片数据
silhouette_colorNo剪影颜色,十六进制格式如 #000000(黑色)#000000
background_colorNo背景颜色,十六进制格式或 'transparent'(透明)transparent
thresholdNo阈值,范围 0-255,用于确定剪影边界
output_formatNo输出格式:PNG、JPEG、WEBP 等PNG

Implementation Reference

  • Core implementation of the create_silhouette tool. Processes the input image by thresholding alpha channel pixels and filling with silhouette color, handles background, and outputs base64 image.
    async def create_silhouette(arguments: Dict[str, Any]) -> List[TextContent]:
        """
        创建图片的剪影效果
        
        Args:
            arguments: 包含图片源和剪影参数的字典
            
        Returns:
            List[TextContent]: 处理结果
        """
        try:
            # 参数验证
            image_source = arguments.get("image_source")
            ensure_valid_image_source(image_source)
            
            silhouette_color = arguments.get("silhouette_color", "#000000")
            background_color = arguments.get("background_color", "transparent")
            threshold = arguments.get("threshold", 128)
            output_format = arguments.get("output_format", DEFAULT_IMAGE_FORMAT)
            
            # 验证参数
            validate_color_hex(silhouette_color)
            if background_color != "transparent":
                validate_color_hex(background_color)
            validate_numeric_range(threshold, 0, 255, "threshold")
            
            # 加载图片
            processor = ImageProcessor()
            image = processor.load_image(image_source)
            
            # 转换为RGBA模式
            if image.mode != "RGBA":
                image = image.convert("RGBA")
            
            # 创建剪影
            silhouette = Image.new("RGBA", image.size, (0, 0, 0, 0))
            
            # 获取像素数据
            pixels = image.load()
            silhouette_pixels = silhouette.load()
            
            # 解析剪影颜色
            silhouette_rgb = tuple(int(silhouette_color[i:i+2], 16) for i in (1, 3, 5))
            
            for y in range(image.height):
                for x in range(image.width):
                    r, g, b, a = pixels[x, y]
                    
                    # 如果像素不透明度大于阈值,则设为剪影颜色
                    if a > threshold:
                        silhouette_pixels[x, y] = silhouette_rgb + (255,)
                    else:
                        silhouette_pixels[x, y] = (0, 0, 0, 0)
            
            # 处理背景
            if background_color != "transparent":
                background_rgb = tuple(int(background_color[i:i+2], 16) for i in (1, 3, 5))
                final_image = Image.new("RGB", image.size, background_rgb)
                final_image.paste(silhouette, (0, 0), silhouette)
            else:
                final_image = silhouette
            
            # 转换为base64
            output_info = processor.output_image(final_image, "silhouette", output_format)
            
            return [TextContent(
                type="text",
                text=json.dumps({
                    "success": True,
                    "message": "成功创建剪影效果",
                    "data": {
                        **output_info,
                        "metadata": {
                            "size": f"{image.width}x{image.height}",
                            "silhouette_color": silhouette_color,
                            "background_color": background_color,
                            "threshold": threshold,
                            "format": output_format
                        }
                    }
                }, ensure_ascii=False)
            )]
            
        except ValidationError as e:
            return [TextContent(
                type="text",
                text=json.dumps({
                    "success": False,
                    "error": f"参数验证失败: {str(e)}"
                }, ensure_ascii=False)
            )]
        except Exception as e:
            return [TextContent(
                type="text",
                text=json.dumps({
                    "success": False,
                    "error": f"创建剪影失败: {str(e)}"
                }, ensure_ascii=False)
            )]
  • main.py:517-541 (registration)
    MCP tool registration for create_silhouette using FastMCP's @mcp.tool() decorator. Defines the input schema with pydantic validations and delegates execution to the handler in tools/effects.py.
    @mcp.tool()
    def create_silhouette(
        image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")],
        silhouette_color: Annotated[str, Field(description="剪影颜色,十六进制格式如 #000000(黑色)", default="#000000")],
        background_color: Annotated[str, Field(description="背景颜色,十六进制格式或 'transparent'(透明)", default="transparent")],
        threshold: Annotated[int, Field(description="阈值,范围 0-255,用于确定剪影边界", ge=0, le=255, default=128)],
        output_format: Annotated[str, Field(description="输出格式:PNG、JPEG、WEBP 等", default="PNG")]
    ) -> str:
        """创建图片的剪影效果"""
        try:
            arguments = {
                "image_source": image_source,
                "silhouette_color": silhouette_color,
                "background_color": background_color,
                "threshold": threshold,
                "output_format": output_format
            }
            result = safe_run_async(effects_create_silhouette(arguments))
            return result[0].text
        except Exception as e:
            return json.dumps({
                "success": False,
                "error": f"创建剪影失败: {str(e)}"
            }, ensure_ascii=False, indent=2)
  • JSON schema definition for create_silhouette tool inputs, defined in get_effect_tools() function (though registration uses pydantic fields in main.py).
    Tool(
        name="create_silhouette",
        description="创建图片的剪影效果",
        inputSchema={
            "type": "object",
            "properties": {
                "image_source": {
                    "type": "string",
                    "description": "图片源(文件路径或base64编码)"
                },
                "silhouette_color": {
                    "type": "string",
                    "description": "剪影颜色(十六进制格式)",
                    "default": "#000000"
                },
                "background_color": {
                    "type": "string",
                    "description": "背景颜色(十六进制格式,transparent表示透明)",
                    "default": "transparent"
                },
                "threshold": {
                    "type": "integer",
                    "description": "透明度阈值(0-255)",
                    "minimum": 0,
                    "maximum": 255,
                    "default": 128
                },
                "output_format": {
                    "type": "string",
                    "description": "输出格式",
                    "enum": ["PNG", "JPEG", "WEBP"],
                    "default": "PNG"
                }
            },
            "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