Skip to main content
Glama

create_silhouette

Generate silhouette effects from images by converting them into defined shapes with customizable silhouette and background colors, enhancing visual distinction for creative or analytical purposes.

Instructions

创建图片的剪影效果

Input Schema

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

Implementation Reference

  • The core handler function that implements the silhouette creation logic: loads image, converts to RGBA, iterates pixels based on alpha threshold > threshold to fill with silhouette_color, handles background, outputs base64 with metadata.
    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) )]
  • Defines the Tool schema for create_silhouette including inputSchema with properties: image_source (required), silhouette_color, background_color, threshold, output_format.
    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"] } ),
  • main.py:518-540 (registration)
    Registers the create_silhouette tool in the FastMCP server. Defines input parameters with descriptions and defaults matching the schema, constructs arguments dict, calls the effects handler via safe_run_async, returns JSON result.
    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)

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