Skip to main content
Glama

resize_image

Adjust image dimensions by specifying width, height, and aspect ratio. Supports various resampling algorithms for quality, smoothness, or speed. Accepts file paths or base64 image data.

Instructions

调整图片大小

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
heightYes目标高度(像素)
image_sourceYes图片源,可以是文件路径或base64编码的图片数据
keep_aspect_ratioNo是否保持宽高比,True时会按比例缩放
resampleNo重采样算法:LANCZOS(高质量)、BILINEAR(平滑)、NEAREST(快速)LANCZOS
widthYes目标宽度(像素)

Implementation Reference

  • The core asynchronous handler function that implements the image resizing logic using PIL. It loads the image, validates parameters, computes target dimensions (optionally preserving aspect ratio), resizes the image, and returns a JSON response with the base64-encoded result.
    async def resize_image(image_source: str, width: int, height: int, keep_aspect_ratio: bool = True, resample: str = "LANCZOS") -> list[TextContent]: """ 调整图片大小 Args: image_source: 图片源(文件路径或base64编码数据) width: 目标宽度 height: 目标高度 keep_aspect_ratio: 是否保持宽高比 resample: 重采样方法 Returns: 调整后的图片数据 """ try: # 验证参数 if not image_source: raise ValidationError("图片源不能为空") ensure_valid_dimensions(width, height) # 验证重采样方法 resample_methods = { 'NEAREST': Image.NEAREST, 'BILINEAR': Image.BILINEAR, 'BICUBIC': Image.BICUBIC, 'LANCZOS': Image.LANCZOS } if resample.upper() not in resample_methods: raise ValidationError(f"不支持的重采样方法: {resample}") # 加载图片 image = processor.load_image(image_source) original_size = image.size # 计算目标尺寸 if keep_aspect_ratio: # 保持宽高比,计算实际尺寸 aspect_ratio = original_size[0] / original_size[1] target_aspect_ratio = width / height if aspect_ratio > target_aspect_ratio: # 以宽度为准 new_width = width new_height = int(width / aspect_ratio) else: # 以高度为准 new_height = height new_width = int(height * aspect_ratio) else: new_width, new_height = width, height # 调整大小 resized_image = image.resize( (new_width, new_height), resample_methods[resample.upper()] ) # 输出调整后的图片 output_info = processor.output_image(resized_image, f"resize_{new_width}x{new_height}") result = { "success": True, "message": f"图片大小调整成功: {original_size} -> {(new_width, new_height)}", "data": { **output_info, "original_size": original_size, "new_size": (new_width, new_height), "keep_aspect_ratio": keep_aspect_ratio, "resample_method": resample } } 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:170-186 (registration)
    The FastMCP tool registration decorator (@mcp.tool()) that defines the synchronous wrapper function for resize_image. It uses Pydantic's Annotated types for input schema/validation and calls the core async handler via safe_run_async.
    @mcp.tool() def resize_image( image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")], width: Annotated[int, Field(description="目标宽度(像素)", gt=0)], height: Annotated[int, Field(description="目标高度(像素)", gt=0)], keep_aspect_ratio: Annotated[bool, Field(description="是否保持宽高比,True时会按比例缩放", default=True)], resample: Annotated[str, Field(description="重采样算法:LANCZOS(高质量)、BILINEAR(平滑)、NEAREST(快速)", default="LANCZOS")] ) -> str: """调整图片大小""" try: result = safe_run_async(transform_resize_image(image_source, width, height, keep_aspect_ratio, resample)) return result[0].text except Exception as e: return json.dumps({ "success": False, "error": f"调整图片大小失败: {str(e)}" }, ensure_ascii=False, indent=2)
  • Pydantic input schema definitions using Annotated and Field for parameter validation, descriptions, constraints, and defaults in the MCP tool registration.
    image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")], width: Annotated[int, Field(description="目标宽度(像素)", gt=0)], height: Annotated[int, Field(description="目标高度(像素)", gt=0)], keep_aspect_ratio: Annotated[bool, Field(description="是否保持宽高比,True时会按比例缩放", default=True)], resample: Annotated[str, Field(description="重采样算法:LANCZOS(高质量)、BILINEAR(平滑)、NEAREST(快速)", default="LANCZOS")]

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