transform_image_from_file
Modify existing image files using text prompts with Google's Gemini AI. Upload an image and describe changes to generate transformed versions automatically saved locally.
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
- src/gemini_image_mcp/server.py:303-342 (handler)The @mcp.tool()-decorated function that serves as both the registration and the handler for the 'transform_image_from_file' tool. It validates the image file path, loads the image using PIL, 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) -> 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
- Helper function called by the tool handler to create transformation instructions and process the image with Gemini via process_image_with_gemini.async def process_image_transform( source_image: PIL.Image.Image, optimized_edit_prompt: str, original_edit_prompt: str ) -> str: """Process image transformation with Gemini. Args: source_image: PIL Image object to transform optimized_edit_prompt: Optimized text prompt for transformation original_edit_prompt: Original user prompt for naming Returns: Path to the transformed image file """ # Create prompt for image transformation edit_instructions = get_image_transformation_prompt(optimized_edit_prompt) # Process with Gemini and return the result return await process_image_with_gemini( [edit_instructions, source_image], original_edit_prompt )