Skip to main content
Glama
duke0317

Image Processing MCP Server

by duke0317

create_collage

Combine multiple images into a single collage with customizable layouts, spacing, and background colors for visual organization or presentation.

Instructions

创建图片拼贴

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
image_sourcesYes图片源列表,每个元素可以是文件路径或base64编码的图片数据
layoutNo布局方式:grid(网格)、horizontal(水平)、vertical(垂直)grid
spacingNo图片间距(像素)
background_colorNo背景颜色,支持十六进制颜色代码#FFFFFF
output_formatNo输出格式:PNG、JPEG、WEBP 等PNG

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • Core implementation of the create_collage tool: loads multiple images, arranges them in specified layout (grid, horizontal, vertical, mosaic), scales to fit max dimensions, creates composite image, and returns base64 output.
    async def create_collage(arguments: Dict[str, Any]) -> List[TextContent]:
        """
        创建图片拼贴
        
        Args:
            arguments: 包含图片源列表和拼贴参数的字典
            
        Returns:
            List[TextContent]: 处理结果
        """
        try:
            # 参数验证
            image_sources = arguments.get("image_sources", [])
            if len(image_sources) < 2:
                raise ValidationError("至少需要2张图片")
            
            layout = arguments.get("layout", "grid")
            spacing = arguments.get("spacing", 10)
            background_color = arguments.get("background_color", "#FFFFFF")
            max_width = arguments.get("max_width", 1200)
            max_height = arguments.get("max_height", 1200)
            output_format = arguments.get("output_format", DEFAULT_IMAGE_FORMAT)
            
            # 验证参数
            validate_numeric_range(spacing, 0, 50, "spacing")
            validate_color_hex(background_color)
            validate_numeric_range(max_width, 200, MAX_IMAGE_SIZE, "max_width")
            validate_numeric_range(max_height, 200, MAX_IMAGE_SIZE, "max_height")
            
            processor = ImageProcessor()
            images = []
            
            # 加载所有图片
            for source in image_sources:
                ensure_valid_image_source(source)
                image = processor.load_image(source)
                images.append(image)
            
            # 根据布局创建拼贴
            if layout == "horizontal":
                # 水平排列
                total_width = sum(img.width for img in images) + spacing * (len(images) - 1)
                max_height_img = max(img.height for img in images)
                
                # 缩放以适应最大尺寸
                if total_width > max_width:
                    scale = max_width / total_width
                    images = [img.resize((int(img.width * scale), int(img.height * scale)), 
                                       Image.Resampling.LANCZOS) for img in images]
                    total_width = max_width
                    max_height_img = max(img.height for img in images)
                
                collage = Image.new("RGB", (total_width, max_height_img), background_color)
                x_offset = 0
                
                for img in images:
                    y_offset = (max_height_img - img.height) // 2
                    collage.paste(img, (x_offset, y_offset))
                    x_offset += img.width + spacing
                    
            elif layout == "vertical":
                # 垂直排列
                max_width_img = max(img.width for img in images)
                total_height = sum(img.height for img in images) + spacing * (len(images) - 1)
                
                # 缩放以适应最大尺寸
                if total_height > max_height:
                    scale = max_height / total_height
                    images = [img.resize((int(img.width * scale), int(img.height * scale)), 
                                       Image.Resampling.LANCZOS) for img in images]
                    max_width_img = max(img.width for img in images)
                    total_height = max_height
                
                collage = Image.new("RGB", (max_width_img, total_height), background_color)
                y_offset = 0
                
                for img in images:
                    x_offset = (max_width_img - img.width) // 2
                    collage.paste(img, (x_offset, y_offset))
                    y_offset += img.height + spacing
                    
            else:  # grid 或 mosaic
                # 网格排列
                import math
                cols = math.ceil(math.sqrt(len(images)))
                rows = math.ceil(len(images) / cols)
                
                # 计算每个单元格的大小
                cell_width = (max_width - spacing * (cols - 1)) // cols
                cell_height = (max_height - spacing * (rows - 1)) // rows
                
                # 调整所有图片到单元格大小
                resized_images = []
                for img in images:
                    img.thumbnail((cell_width, cell_height), Image.Resampling.LANCZOS)
                    resized_images.append(img)
                
                # 创建拼贴
                collage_width = cols * cell_width + spacing * (cols - 1)
                collage_height = rows * cell_height + spacing * (rows - 1)
                collage = Image.new("RGB", (collage_width, collage_height), background_color)
                
                for i, img in enumerate(resized_images):
                    row = i // cols
                    col = i % cols
                    
                    x = col * (cell_width + spacing)
                    y = row * (cell_height + spacing)
                    
                    # 居中放置图片
                    x_offset = x + (cell_width - img.width) // 2
                    y_offset = y + (cell_height - img.height) // 2
                    
                    collage.paste(img, (x_offset, y_offset))
            
            # 转换为base64
            output_info = processor.output_image(collage, "batch_resize", output_format)
            
            return [TextContent(
                type="text",
                text=json.dumps({
                    "success": True,
                    "message": f"成功创建{layout}拼贴",
                    "data": {
                        **output_info,
                        "metadata": {
                            "image_count": len(images),
                            "layout": layout,
                            "size": f"{collage.width}x{collage.height}",
                            "spacing": spacing,
                            "background_color": background_color,
                            "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)
            )]
  • Tool registration in get_advanced_tools() including name, description, and detailed inputSchema for validation.
    Tool(
        name="create_collage",
        description="创建图片拼贴",
        inputSchema={
            "type": "object",
            "properties": {
                "image_sources": {
                    "type": "array",
                    "description": "图片源列表(文件路径或base64编码)",
                    "items": {"type": "string"},
                    "minItems": 2,
                    "maxItems": 9
                },
                "layout": {
                    "type": "string",
                    "description": "拼贴布局",
                    "enum": ["grid", "horizontal", "vertical", "mosaic"],
                    "default": "grid"
                },
                "spacing": {
                    "type": "integer",
                    "description": "图片间距(像素)",
                    "minimum": 0,
                    "maximum": 50,
                    "default": 10
                },
                "background_color": {
                    "type": "string",
                    "description": "背景颜色(十六进制格式)",
                    "default": "#FFFFFF"
                },
                "max_width": {
                    "type": "integer",
                    "description": "最大宽度",
                    "minimum": 200,
                    "maximum": MAX_IMAGE_SIZE,
                    "default": 1200
                },
                "max_height": {
                    "type": "integer",
                    "description": "最大高度",
                    "minimum": 200,
                    "maximum": MAX_IMAGE_SIZE,
                    "default": 1200
                },
                "output_format": {
                    "type": "string",
                    "description": "输出格式",
                    "enum": ["PNG", "JPEG", "WEBP"],
                    "default": "PNG"
                }
            },
            "required": ["image_sources"]
        }
    ),
  • main.py:674-697 (registration)
    Final MCP server registration of the create_collage tool using @mcp.tool() decorator, which wraps the advanced implementation call.
    @mcp.tool()
    def create_collage(
        image_sources: Annotated[list, Field(description="图片源列表,每个元素可以是文件路径或base64编码的图片数据")],
        layout: Annotated[str, Field(description="布局方式:grid(网格)、horizontal(水平)、vertical(垂直)", default="grid")],
        spacing: Annotated[int, Field(description="图片间距(像素)", ge=0, default=10)],
        background_color: Annotated[str, Field(description="背景颜色,支持十六进制颜色代码", default="#FFFFFF")],
        output_format: Annotated[str, Field(description="输出格式:PNG、JPEG、WEBP 等", default="PNG")]
    ) -> str:
        """创建图片拼贴"""
        try:
            arguments = {
                "image_sources": image_sources,
                "layout": layout,
                "spacing": spacing,
                "background_color": background_color,
                "output_format": output_format
            }
            result = safe_run_async(advanced_create_collage(arguments))
            return result[0].text
        except Exception as e:
            return json.dumps({
                "success": False,
                "error": f"创建拼贴失败: {str(e)}"
            }, ensure_ascii=False, indent=2)
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden for behavioral disclosure. The description reveals nothing about what the tool actually does behaviorally - no information about permissions needed, whether it modifies source images, rate limits, output characteristics beyond format, error conditions, or performance implications. It's a minimal phrase that fails to describe the tool's operational behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise - a single Chinese phrase that directly states the tool's function. While it's under-specified for completeness, as pure conciseness it's maximally efficient with zero wasted words or redundant information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool has 5 parameters, no annotations, and operates in a domain with many similar sibling tools, the description is severely incomplete. While an output schema exists (which helps with return values), the description fails to provide necessary context about when to use this tool, what it actually produces, or how it differs from alternatives. For a creative/mutation tool with multiple parameters, this minimal description is inadequate.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 100% schema description coverage, all parameters are well-documented in the input schema itself. The description adds no parameter information beyond what's already in the schema - it doesn't explain relationships between parameters, provide usage examples, or clarify edge cases. The baseline of 3 is appropriate since the schema does the heavy lifting, but the description contributes nothing additional.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose2/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description '创建图片拼贴' (Create image collage) is a tautology that essentially restates the tool name 'create_collage' in Chinese. It provides no additional specificity about what kind of collage, what resources it uses, or how it differs from sibling tools like 'create_thumbnail_grid' or 'blend_images'. The purpose is stated but lacks distinguishing details.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides absolutely no guidance on when to use this tool versus alternatives. With multiple sibling tools for image manipulation (e.g., 'create_thumbnail_grid', 'blend_images', 'create_gif'), there is no indication of when this specific collage creation tool is appropriate versus other composition or formatting tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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