Gemini Image Generation
Generates images via the Google Gemini API and saves them to disk as PNG files.
Prerequisites
GEMINI_API_KEY environment variable set — see below if not set
- Go 1.22+ installed (
go version to verify)
Setting up GEMINI_API_KEY
Before generating images, check whether the key is set:
# Check if set
echo $env:GEMINI_API_KEY # PowerShell
echo $GEMINI_API_KEY # bash/zsh
If it's empty or missing, prompt the user:
"To generate images, you need a Google AI Studio API key. It's free — visit
https://aistudio.google.com/apikey, sign in, click Create API key, and copy it.
Then set it in your terminal:"
PowerShell (current session):
$env:GEMINI_API_KEY = "your-key-here"
PowerShell (permanent, user profile):
[System.Environment]::SetEnvironmentVariable("GEMINI_API_KEY", "your-key-here", "User")
bash/zsh (add to ~/.bashrc or ~/.zshrc for persistence):
export GEMINI_API_KEY="your-key-here"
Once the user confirms the key is set, proceed with image generation.
Available scripts
scripts/ — Image generation CLI (Go, self-contained with inline dependencies)
Usage
Run from the scripts/ directory (where go.mod lives):
cd scripts
go run . [flags] <prompt words...>
Flags
| Flag |
Default |
Description |
-o, --output FILE |
auto (image-NNN.png) |
Output file path |
-m, --model MODEL |
gemini-3.1-flash-image-preview |
Gemini model |
--aspect RATIO |
1:1 |
Aspect ratio |
Models
| Model |
Best for |
gemini-3.1-flash-image-preview |
Best all-round quality/speed (default) |
gemini-2.5-flash-image |
Fast, high-volume, lower cost |
gemini-3-pro-image-preview |
Professional assets, complex prompts, up to 4K |
Aspect ratios
1:1 16:9 9:16 3:2 2:3 4:3 3:4 4:5 5:4 21:9
Output
- Prints the saved file path to stdout on success — capture it for use in other steps
- Progress and diagnostics go to stderr
- Exit codes:
0 success · 1 invalid args · 2 API error · 3 write error
Examples
cd scripts
# Blog hero image
go run . --aspect 16:9 --output hero.png \
"photorealistic misty mountain range at golden hour, wide angle shot"
# Icon or sticker
go run . --aspect 1:1 --output icon.png \
"minimalist Go gopher mascot, flat design, white background, no text"
# Portrait illustration
go run . --aspect 2:3 --output portrait.png \
"watercolor portrait of a software engineer, warm tones, abstract background"
# Product shot
go run . -m gemini-3-pro-image-preview --aspect 3:2 --output product.png \
"studio-lit product photograph of a ceramic coffee mug on a marble surface, three-point softbox lighting"
# Capture the output path for use in a pipeline
$imgPath = go run . "abstract dark blue tech background, geometric"
Agent workflow
- Compose a detailed prompt — include subject, style, lighting, mood, and any text to render
- Choose aspect ratio — match the intended use (
16:9 for headers, 1:1 for icons, 9:16 for mobile)
cd into the skill's scripts/ directory, then run go run . capturing stdout as the file path
- Use the returned path in subsequent steps (embed in HTML, commit to repo, attach to PR, etc.)
Prompting tips
- Describe the scene narratively, not as a keyword list — the model understands context
- For photorealistic results, use camera terms:
wide-angle shot, macro, golden hour, soft bokeh
- For text in images, describe the font style:
bold sans-serif, elegant serif; use gemini-3-pro-image-preview for reliable text rendering
- For icons/stickers, specify
white background, no text and a clear style like flat design or 3D clay
- Iterate: run once, then refine the prompt and run again
1---2name: gemini-image3description: Generate images using the Google Gemini Image Generation API. Use when an agent needs to create, illustrate, or visualize anything — icons, hero images, diagrams, concept art, product shots, illustrations. Activates on "generate image", "create an image", "illustrate this", "make a picture of", "draw", or any request to produce a visual asset.4---56# Gemini Image Generation78Generates images via the Google Gemini API and saves them to disk as PNG files.910## Prerequisites1112- `GEMINI_API_KEY` environment variable set — see below if not set13- Go 1.22+ installed (`go version` to verify)1415## Setting up GEMINI_API_KEY1617Before generating images, check whether the key is set:1819```bash20# Check if set21echo $env:GEMINI_API_KEY # PowerShell22echo $GEMINI_API_KEY # bash/zsh23```2425If it's empty or missing, prompt the user:2627> "To generate images, you need a Google AI Studio API key. It's free — visit28> https://aistudio.google.com/apikey, sign in, click **Create API key**, and copy it.29> Then set it in your terminal:"30>31> **PowerShell (current session):**32> ```powershell33> $env:GEMINI_API_KEY = "your-key-here"34> ```35>36> **PowerShell (permanent, user profile):**37> ```powershell38> [System.Environment]::SetEnvironmentVariable("GEMINI_API_KEY", "your-key-here", "User")39> ```40>41> **bash/zsh (add to ~/.bashrc or ~/.zshrc for persistence):**42> ```bash43> export GEMINI_API_KEY="your-key-here"44> ```4546Once the user confirms the key is set, proceed with image generation.4748## Available scripts4950- **`scripts/`** — Image generation CLI (Go, self-contained with inline dependencies)5152## Usage5354Run from the **`scripts/`** directory (where `go.mod` lives):5556```bash57cd scripts58go run . [flags] <prompt words...>59```6061### Flags6263| Flag | Default | Description |64|---|---|---|65| `-o`, `--output FILE` | auto (`image-NNN.png`) | Output file path |66| `-m`, `--model MODEL` | `gemini-3.1-flash-image-preview` | Gemini model |67| `--aspect RATIO` | `1:1` | Aspect ratio |6869### Models7071| Model | Best for |72|---|---|73| `gemini-3.1-flash-image-preview` | Best all-round quality/speed **(default)** |74| `gemini-2.5-flash-image` | Fast, high-volume, lower cost |75| `gemini-3-pro-image-preview` | Professional assets, complex prompts, up to 4K |7677### Aspect ratios7879`1:1` `16:9` `9:16` `3:2` `2:3` `4:3` `3:4` `4:5` `5:4` `21:9`8081## Output8283- Prints the **saved file path** to **stdout** on success — capture it for use in other steps84- Progress and diagnostics go to **stderr**85- Exit codes: `0` success · `1` invalid args · `2` API error · `3` write error8687## Examples8889```bash90cd scripts9192# Blog hero image93go run . --aspect 16:9 --output hero.png \94 "photorealistic misty mountain range at golden hour, wide angle shot"9596# Icon or sticker97go run . --aspect 1:1 --output icon.png \98 "minimalist Go gopher mascot, flat design, white background, no text"99100# Portrait illustration101go run . --aspect 2:3 --output portrait.png \102 "watercolor portrait of a software engineer, warm tones, abstract background"103104# Product shot105go run . -m gemini-3-pro-image-preview --aspect 3:2 --output product.png \106 "studio-lit product photograph of a ceramic coffee mug on a marble surface, three-point softbox lighting"107108# Capture the output path for use in a pipeline109$imgPath = go run . "abstract dark blue tech background, geometric"110```111112## Agent workflow1131141. **Compose a detailed prompt** — include subject, style, lighting, mood, and any text to render1152. **Choose aspect ratio** — match the intended use (`16:9` for headers, `1:1` for icons, `9:16` for mobile)1163. **`cd` into the skill's `scripts/` directory**, then run `go run .` capturing stdout as the file path1174. **Use the returned path** in subsequent steps (embed in HTML, commit to repo, attach to PR, etc.)118119## Prompting tips120121- Describe the scene narratively, not as a keyword list — the model understands context122- For photorealistic results, use camera terms: `wide-angle shot`, `macro`, `golden hour`, `soft bokeh`123- For text in images, describe the font style: `bold sans-serif`, `elegant serif`; use `gemini-3-pro-image-preview` for reliable text rendering124- For icons/stickers, specify `white background, no text` and a clear style like `flat design` or `3D clay`125- Iterate: run once, then refine the prompt and run again