transform_image_from_file
Modify existing images using text prompts with Google's Gemini model. Input an image file and a descriptive prompt to generate and save a transformed version 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-343 (handler)The primary handler function for the 'transform_image_from_file' tool. It validates the image file path, loads the image using PIL, translates the prompt using Gemini, processes the image transformation via helper functions, saves the result, and returns the file path. Registration via @mcp.tool() decorator.@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
- Key helper function that handles the core Gemini API call for image transformation by preparing the edit instructions prompt and invoking the shared image processing logic.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 )
- Helper function that translates the user prompt to English using Gemini for optimal image generation results.async def translate_prompt(text: str) -> str: """Translate and optimize the user's prompt to English for better image generation results. Args: text: The original prompt in any language Returns: English translation of the prompt with preserved intent """ try: # Create a prompt for translation with strict intent preservation prompt = get_translate_prompt(text) # Call Gemini and get the translated prompt translated_prompt = await call_gemini(prompt, text_only=True) logger.info(f"Original prompt: {text}") logger.info(f"Translated prompt: {translated_prompt}") return translated_prompt except Exception as e: logger.error(f"Error translating prompt: {str(e)}") # Return original text if translation fails return text