transform_image_from_file
Modify existing images using text prompts with Google's Gemini model. Provide an image file path and descriptive prompt to transform images.
Instructions
Transform an existing image file based on the given text prompt using Google's Gemini model.
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
- The handler function for the 'transform_image_from_file' tool. It is registered via the @mcp.tool() decorator. Loads the image from file path, translates the prompt, and delegates to process_image_transform for Gemini API interaction.@mcp.tool() async def transform_image_from_file(image_file_path: str, prompt: str) -> Tuple[bytes, str]: """Transform an existing image file based on the given text prompt using Google's Gemini model. 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: logger.info(f"Processing transform_image_from_file request with prompt: {prompt}") logger.info(f"Image file path: {image_file_path}") # Validate file path if not os.path.exists(image_file_path): raise ValueError(f"Image file not found: {image_file_path}") # Translate the prompt to English translated_prompt = await translate_prompt(prompt) # 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 # Process the transformation return await process_image_transform(source_image, translated_prompt, prompt) except Exception as e: error_msg = f"Error transforming image: {str(e)}" logger.error(error_msg) return error_msg