Generate or edit images using AI models (FLUX, Gemini). Use for scientific illustrations, diagrams, schematics, infographics, concept visualizations, and artistic images. Supports image editing to modify existing images (change colors, add/remove elements, style transfer). Useful for figures, posters, and visual explanations.
Generate and edit high-quality images using OpenRouter's image generation models including FLUX.2 Pro and Nano Banana Pro (Gemini 3 Pro).
Use the scripts/generate_image.py script to generate or edit images:
# Generate a new image
python scripts/generate_image.py "A beautiful sunset over mountains"
# Edit an existing image
python scripts/generate_image.py "Make the sky purple" --input photo.jpg
This generates/edits an image and saves it as generated_image.png in the current directory.
CRITICAL: The script requires an OpenRouter API key. Before running, check if the user has configured their API key:
.env file in the project directory or parent directoriesOPENROUTER_API_KEY=<key> in the .env file.env file with OPENROUTER_API_KEY=your-api-key-hereexport OPENROUTER_API_KEY=your-api-key-hereThe script will automatically detect the .env file and provide clear error messages if the API key is missing.
Default model: google/gemini-3-pro-image-preview (high quality, recommended)
Available models for generation and editing:
google/gemini-3-pro-image-preview - High quality, supports generation + editingblack-forest-labs/flux.2-pro - Fast, high quality, supports generation + editingGeneration only:
black-forest-labs/flux.2-dev - Development version, generation onlySelect based on:
python scripts/generate_image.py "Your prompt here"
python scripts/generate_image.py "A cat in space" --model "black-forest-labs/flux.2-pro"
python scripts/generate_image.py "Abstract art" --output artwork.png
python scripts/generate_image.py "Make the background blue" --input photo.jpg
python scripts/generate_image.py "Add sunglasses to the person" --input portrait.png --model "black-forest-labs/flux.2-pro"
python scripts/generate_image.py "Remove the text from the image" --input screenshot.png --output cleaned.png
Run the script multiple times with different prompts or output paths:
python scripts/generate_image.py "Image 1 description" --output image1.png
python scripts/generate_image.py "Image 2 description" --output image2.png
prompt (required): Text description of the image to generate, or editing instructions--input or -i: Input image path for editing (enables edit mode)--model or -m: OpenRouter model ID (default: google/gemini-3-pro-image-preview)--output or -o: Output file path (default: generated_image.png)--api-key: OpenRouter API key (overrides .env file)The script provides clear error messages for:
If the script fails, read the error message and address the issue before retrying.
images and content response formats from different OpenRouter models