#!/usr/bin/env python3
"""Generate an image using the MCP server and save it to a file for viewing."""
import asyncio
import sys
from pathlib import Path
from dotenv import load_dotenv
# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
# Load environment variables from .env
load_dotenv(project_root / ".env")
async def generate_and_save_image():
"""Generate an image and save it to a file."""
from Imagen_MCP.tools.generate_image import generate_image
# Create output directory
output_dir = project_root / "output"
output_dir.mkdir(exist_ok=True)
output_path = output_dir / "generated_image.png"
print("Generating image with Imagen MCP Server...")
print("Prompt: A beautiful sunset over mountains with a lake in the foreground")
print("Model: imagen-4-fast")
print(f"Output: {output_path}")
print()
result = await generate_image(
prompt="A beautiful sunset over mountains with a lake in the foreground, photorealistic",
output_path=str(output_path),
model="imagen-4-fast",
size="1024x1024",
)
if not result.success:
print(f"ERROR: Image generation failed: {result.error}")
return None
print("Image generated successfully!")
print(f"Model used: {result.model_used}")
print(f"File saved to: {result.file_path}")
print(f"File size: {result.file_size_bytes} bytes")
if result.revised_prompt:
print(f"Revised prompt: {result.revised_prompt}")
return Path(result.file_path) if result.file_path else None
async def main():
"""Main entry point."""
output_path = await generate_and_save_image()
if output_path:
print(f"\nTo view the image, open: file://{output_path.absolute()}")
return 0
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)