transform_image_from_file
Modify existing image files using text prompts to apply transformations, edits, or creative changes through AI image generation providers.
Instructions
Transform an existing image file based on the given text prompt using the configured image provider.
Args:
image_file_path: Path to the image file to be transformed
prompt: Text prompt describing the desired transformation or modifications
Returns:
Path to the transformed image file saved on the server
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_file_path | Yes | ||
| prompt | Yes |
Implementation Reference
- Primary handler and registration for the transform_image_from_file tool via @mcp.tool(). Loads image from local file path, prepares provider-optimized transformation prompt, invokes provider.transform_image, saves result, and returns file path.@mcp.tool() async def transform_image_from_file(image_file_path: str, prompt: str) -> str: """Transform an existing image file based on the given text prompt using the configured image provider. Args: image_file_path: Path to the image file to be transformed prompt: Text prompt describing the desired transformation or modifications Returns: Path to the transformed image file saved on the server """ try: provider = get_image_provider() logger.info(f"Processing transform_image_from_file request with {provider.get_name()}") logger.info(f"Image file path: {image_file_path}") logger.info(f"Transformation prompt: {prompt}") # Validate file path if not os.path.exists(image_file_path): raise ValueError(f"Image file not found: {image_file_path}") # Load the source image directly using PIL try: source_image = PIL.Image.open(image_file_path) logger.info(f"Successfully loaded image from file: {image_file_path}") except PIL.UnidentifiedImageError: logger.error("Error: Could not identify image format") raise ValueError("Could not identify image format. Supported formats include PNG, JPEG, GIF, WebP.") except Exception as e: logger.error(f"Error: Could not load image: {str(e)}") raise # Translate the prompt for the provider translated_prompt = await translate_prompt_for_provider(prompt, provider) # Create detailed transformation prompt transformation_prompt = get_image_transformation_prompt(translated_prompt) # Process the transformation using the provider _, saved_path, remote_url = await provider.transform_image(source_image, transformation_prompt) logger.info(f"Image transformed and saved to: {saved_path}") # Prepare response with remote URL if available response = f"Image transformed and saved to: {saved_path}" if remote_url: response += f"\nRemote URL: {remote_url}" return response except Exception as e: error_msg = f"Error transforming image: {str(e)}" logger.error(error_msg) return error_msg