process_image
Apply post-processing operations to images including resizing, background removal, and outline addition for game asset preparation.
Instructions
Apply post-processing operations to an image.
Args:
image_base64: Base64 encoded input image
operations: List of operations to apply: "resize", "remove_background", "add_outline"
resize_width: Target width for resize operation
resize_height: Target height for resize operation
outline_color: Hex color for outline (e.g., "#000000")
outline_thickness: Thickness of outline in pixels
save_to_file: Whether to save processed image to disk
Returns:
JSON with processed image as base64
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_base64 | Yes | ||
| operations | Yes | ||
| resize_width | No | ||
| resize_height | No | ||
| outline_color | No | #000000 | |
| outline_thickness | No | ||
| save_to_file | No |
Implementation Reference
- server/main.py:756-807 (handler)The primary handler function for the 'process_image' tool. It applies a sequence of image processing operations (resize, remove background, add outline) to a base64-encoded input image using imported utility functions and returns the result as JSON.@mcp.tool() async def process_image( image_base64: str, operations: List[str], resize_width: Optional[int] = None, resize_height: Optional[int] = None, outline_color: str = "#000000", outline_thickness: int = 1, save_to_file: bool = False ) -> str: """Apply post-processing operations to an image. Args: image_base64: Base64 encoded input image operations: List of operations to apply: "resize", "remove_background", "add_outline" resize_width: Target width for resize operation resize_height: Target height for resize operation outline_color: Hex color for outline (e.g., "#000000") outline_thickness: Thickness of outline in pixels save_to_file: Whether to save processed image to disk Returns: JSON with processed image as base64 """ import base64 image_bytes = base64.b64decode(image_base64) for op in operations: if op == "resize" and resize_width and resize_height: image_bytes = resize_image(image_bytes, resize_width, resize_height) elif op == "remove_background": image_bytes = remove_background(image_bytes) elif op == "add_outline": # Parse hex color color = outline_color.lstrip("#") r, g, b = tuple(int(color[i:i+2], 16) for i in (0, 2, 4)) image_bytes = add_outline(image_bytes, color=(r, g, b, 255), thickness=outline_thickness) result = { "success": True, "image_base64": image_to_base64(image_bytes), "operations_applied": operations } if save_to_file: output_dir = ensure_directory(OUTPUT_DIR / "processed") fname = generate_filename(prefix="processed") file_path = output_dir / fname file_path.write_bytes(image_bytes) result["file_path"] = str(file_path) return json.dumps(result, indent=2)
- server/utils.py:42-53 (helper)Utility function used by process_image for resizing images to specified dimensions.def resize_image( image_bytes: bytes, width: int, height: int, resample: int = Image.Resampling.NEAREST ) -> bytes: """Resize an image to the specified dimensions.""" img = Image.open(BytesIO(image_bytes)) resized = img.resize((width, height), resample=resample) buffer = BytesIO() resized.save(buffer, format="PNG") return buffer.getvalue()
- server/utils.py:121-137 (helper)Utility function used by process_image for removing near-white backgrounds by setting them to transparent.def remove_background(image_bytes: bytes, threshold: int = 10) -> bytes: """Simple background removal by making near-white pixels transparent.""" img = Image.open(BytesIO(image_bytes)).convert("RGBA") data = img.getdata() new_data = [] for item in data: # If pixel is close to white, make it transparent if item[0] > 255 - threshold and item[1] > 255 - threshold and item[2] > 255 - threshold: new_data.append((255, 255, 255, 0)) else: new_data.append(item) img.putdata(new_data) buffer = BytesIO() img.save(buffer, format="PNG") return buffer.getvalue()
- server/utils.py:140-170 (helper)Utility function used by process_image for adding a colored outline around non-transparent pixels.def add_outline( image_bytes: bytes, color: Tuple[int, int, int, int] = (0, 0, 0, 255), thickness: int = 1 ) -> bytes: """Add an outline around non-transparent pixels.""" img = Image.open(BytesIO(image_bytes)).convert("RGBA") # Create outline image outline = Image.new("RGBA", img.size, (0, 0, 0, 0)) pixels = img.load() outline_pixels = outline.load() for y in range(img.height): for x in range(img.width): if pixels[x, y][3] > 0: # Non-transparent pixel # Check neighbors for dy in range(-thickness, thickness + 1): for dx in range(-thickness, thickness + 1): nx, ny = x + dx, y + dy if 0 <= nx < img.width and 0 <= ny < img.height: if pixels[nx, ny][3] == 0: # Transparent neighbor outline_pixels[x, y] = color break # Composite outline under original result = Image.alpha_composite(outline, img) buffer = BytesIO() result.save(buffer, format="PNG") return buffer.getvalue()