Gemini image generation
Requirements
- uv on
PATH. It installs Python and thegoogle-genaidependency on the first run. - A Gemini API key in the environment variable
GEMINI_API_KEY(get one at https://aistudio.google.com/apikey).
Never print, echo, log or write the key's value anywhere; only the script reads it. If it is missing, ask the user to set it themselves in their own terminal and then restart their coding agent, and never to paste it into the chat:
- Windows (PowerShell):
[Environment]::SetEnvironmentVariable("GEMINI_API_KEY", "<key>", "User") - macOS / Linux: add
export GEMINI_API_KEY="<key>"to~/.zshrcor~/.bashrc
Run
Paths are relative to the directory that contains this SKILL.md (<skill-dir> below). Resolve it to an absolute path before running, because the command usually runs from the user's project. uv run --quiet <skill-dir>/scripts/gemini_image.py --help prints every option.
uv run --quiet <skill-dir>/scripts/gemini_image.py \
--model gemini-3.1-flash-image --aspect 1:1 --size 1K \
--out <project>/art/<name>.png \
--prompt "..."
--model(required): see the table below.--aspect(required):1:1,2:3,3:2,3:4,4:3,4:5,5:4,9:16,16:9,21:9.--size(required): output resolution,1K,2Kor4K(uppercaseK). It sets the pixel area (about 1024², 2048² or 4096²), and--aspectsets the shape, so the long side is larger than the nominal size for wide images:2Kat1:1is 2048×2048,2Kat16:9came out 2752×1536. Pick it from the user's request ("4K", "high resolution", "for print"); for drafts and iterations use1K, for final assets and image-to-3D input use2K, and use4Konly when asked, since bigger images cost more and take longer. If a model refuses a size, say so and retry with a supported one only with the user's OK.--out(required): output path. The extension is replaced by the one matching the returned MIME type (Gemini often returns JPEG, sox.pngmay be saved asx.jpg). Extra images are saved as<out>_1,<out>_2, ...--promptor--prompt-file(UTF-8 text, better for long prompts; keep prompt files next to the outputs so results are reproducible).--ref <image>(repeatable): reference images for editing, style matching or keeping a character consistent.
The script prints saved <path> for each image and any text the model returns. It retries rate limits and server errors with exponential backoff and exits non-zero with a clear message when the key is missing, an input file does not exist, the API rejects the request or no image comes back. Look at the result with the Read tool before reporting.
Models (verify with client.models.list() if a name fails)
| Model | Name | Use |
|---|---|---|
gemini-3.1-flash-image | Nano Banana 2 | Default: good quality, fast, cheap. |
gemini-3.1-flash-lite-image | Nano Banana 2 Lite | Many cheap drafts. |
gemini-nano-banana-2.1 | Nano Banana 2.1 | Newer flash tier; try when 2 misses detail. |
gemini-3-pro-image | Nano Banana Pro | Highest quality, text rendering, complex scenes; slower and pricier. |
gemini-2.5-flash-image | Nano Banana | Legacy. |
Prompts for 3D pipelines
For image-to-3D (Meshy and similar): a single subject, full body in frame, neutral A-pose or T-pose for characters, orthographic-looking front view, plain light grey background, even soft lighting, no cast shadows, no props cut off by the frame. For multi-view input, generate the front first, then pass it as --ref and ask for the side and back views in the same style.