Nano Banana 2 Imagegen
Overview
Use the bundled Python script to turn a prompt into one or more saved image files through Google's official google-genai SDK. Require google-genai and Pillow for this skill, and prefer the bundled script over ad-hoc snippets so API key loading, image editing inputs, and output handling stay consistent.
Requirements
Install the required Python packages before using this skill:
pip install -U google-genai pillow
Quick Start
Run the bundled script:
python nano-banana-2-imagegen/scripts/generate_image.py \
"Create a cinematic product photo of a brass fountain pen on dark slate" \
--output /tmp/fountain-pen.png
Edit an existing image:
python nano-banana-2-imagegen/scripts/generate_image.py \
"Turn this sneaker photo into a clean studio ad with a pale blue backdrop" \
--input-image /tmp/sneaker.jpg \
--output /tmp/sneaker-ad.png
Load a prompt from a file and apply post-processing:
python nano-banana-2-imagegen/scripts/generate_image.py \
--prompt-file /tmp/prompt.txt \
--api-key-file ~/.config/gemini/api-key \
--resize 1536x1024 \
--crop 128,0,1408,1024 \
--output /tmp/generated.png
Continue a multi-turn edit across separate invocations:
python nano-banana-2-imagegen/scripts/generate_image.py \
"Create a glossy app icon from this sketch" \
--input-image /tmp/sketch.png \
--state-file /tmp/icon-chat.json \
--output /tmp/icon-v1.png
python nano-banana-2-imagegen/scripts/generate_image.py \
"Keep the icon shape, but make the background cobalt blue and add a soft shadow" \
--state-file /tmp/icon-chat.json \
--output /tmp/icon-v2.png
Workflow
- Resolve the prompt from CLI text or
--prompt-file.
- Resolve the API key from explicit value, environment, or key file.
- Optionally load one or more input images with Pillow for edit requests.
- Call Gemini through the Google GenAI SDK with
gemini-3.1-flash-image-preview by default.
- If
--state-file is set, create or resume a persistent SDK chat for multi-turn editing.
- Optionally apply Pillow post-processing such as crop or resize.
- Save returned image parts to the requested output path.
- Save updated chat history back to the state file when multi-turn mode is enabled.
- Print any model text plus the saved file paths.
Arguments
--input-image: Provide one or more images for image-to-image editing.
--output: Destination file path. If the model returns multiple images, the script appends -2, -3, and so on.
--state-file: Persist full Gemini chat history to JSON so later invocations can continue the same edit conversation.
--reset-state: Ignore any existing state file and start a fresh chat.
--model: Override the model id. Default is gemini-3.1-flash-image-preview.
--api-key: Pass the API key directly.
--api-key-file: Read the API key from a file.
--crop: Apply left,top,right,bottom cropping after generation.
--resize: Apply WIDTHxHEIGHT resizing after generation.
--format: Force the saved format to PNG, JPEG, or WEBP.
--dry-run: Print the resolved request payload without calling the API.
API Key Resolution
Resolve credentials in this order:
--api-key
GEMINI_API_KEY
GOOGLE_API_KEY
--api-key-file
GEMINI_API_KEY_FILE
GOOGLE_API_KEY_FILE
Accept these key file formats:
- Plain text containing only the key
.env style: GEMINI_API_KEY=...
.env style: GOOGLE_API_KEY=...
When to Read References
Read references/gemini-image-generation.md when you need:
- Current model names or their intended roles
- The latest documented SDK usage for generation vs image editing
- A reminder of installation, auth conventions, chat history, or multi-turn image workflows
Read references/prompting-guide.md when:
- The model is producing vague, generic, or inconsistent results
- You need better generation prompts for a product shot, icon, poster, mockup, or hero image
- You need tighter edit prompts that clearly separate what should change from what should stay fixed
- You are doing multi-turn refinement and want prompts that reduce drift between turns
Constraints
- Use the bundled script and the official Google GenAI SDK instead of raw REST calls.
- Prefer
--dry-run first when verifying prompt assembly or key resolution.
- Treat
google-genai and Pillow as required dependencies for this skill.
- Use
--input-image whenever the request is an edit, restyle, or transformation of an existing image.
- Use
--state-file for iterative refinement across separate CLI invocations.
- Use
--reset-state when reusing a state-file path for a completely new concept.
- Keep prompts explicit about subject, composition, lighting, style, and text rendering requirements.
- Treat generated images as local outputs that should be saved to a user-specified path when one is available.
- Do not claim a generation succeeded unless the script actually saved image files.
Files
scripts/generate_image.py: SDK-based CLI for generation, editing, and light post-processing.
references/gemini-image-generation.md: Current Google Gemini image generation notes.
references/prompting-guide.md: Practical prompt-writing patterns for generation, editing, and multi-turn refinement.
Source
1---2name: nano-banana-2-imagegen3description: Generate and edit images with Google's Nano Banana 2 model through the official Python Google GenAI SDK. Use when a user asks to create an image, edit an existing image, restyle a photo, add or remove visual elements, create concept art, mockups, infographics, or product shots with Gemini / Nano Banana, especially when the result should be saved locally and the API key may come from the environment or a file.4---56# Nano Banana 2 Imagegen78## Overview910Use the bundled Python script to turn a prompt into one or more saved image files through Google's official `google-genai` SDK. Require `google-genai` and `Pillow` for this skill, and prefer the bundled script over ad-hoc snippets so API key loading, image editing inputs, and output handling stay consistent.1112## Requirements1314Install the required Python packages before using this skill:1516```bash17pip install -U google-genai pillow18```1920## Quick Start2122Run the bundled script:2324```bash25python nano-banana-2-imagegen/scripts/generate_image.py \26 "Create a cinematic product photo of a brass fountain pen on dark slate" \27 --output /tmp/fountain-pen.png28```2930Edit an existing image:3132```bash33python nano-banana-2-imagegen/scripts/generate_image.py \34 "Turn this sneaker photo into a clean studio ad with a pale blue backdrop" \35 --input-image /tmp/sneaker.jpg \36 --output /tmp/sneaker-ad.png37```3839Load a prompt from a file and apply post-processing:4041```bash42python nano-banana-2-imagegen/scripts/generate_image.py \43 --prompt-file /tmp/prompt.txt \44 --api-key-file ~/.config/gemini/api-key \45 --resize 1536x1024 \46 --crop 128,0,1408,1024 \47 --output /tmp/generated.png48```4950Continue a multi-turn edit across separate invocations:5152```bash53python nano-banana-2-imagegen/scripts/generate_image.py \54 "Create a glossy app icon from this sketch" \55 --input-image /tmp/sketch.png \56 --state-file /tmp/icon-chat.json \57 --output /tmp/icon-v1.png5859python nano-banana-2-imagegen/scripts/generate_image.py \60 "Keep the icon shape, but make the background cobalt blue and add a soft shadow" \61 --state-file /tmp/icon-chat.json \62 --output /tmp/icon-v2.png63```6465## Workflow66671. Resolve the prompt from CLI text or `--prompt-file`.682. Resolve the API key from explicit value, environment, or key file.693. Optionally load one or more input images with Pillow for edit requests.704. Call Gemini through the Google GenAI SDK with `gemini-3.1-flash-image-preview` by default.715. If `--state-file` is set, create or resume a persistent SDK chat for multi-turn editing.726. Optionally apply Pillow post-processing such as crop or resize.737. Save returned image parts to the requested output path.748. Save updated chat history back to the state file when multi-turn mode is enabled.759. Print any model text plus the saved file paths.7677## Arguments7879- `--input-image`: Provide one or more images for image-to-image editing.80- `--output`: Destination file path. If the model returns multiple images, the script appends `-2`, `-3`, and so on.81- `--state-file`: Persist full Gemini chat history to JSON so later invocations can continue the same edit conversation.82- `--reset-state`: Ignore any existing state file and start a fresh chat.83- `--model`: Override the model id. Default is `gemini-3.1-flash-image-preview`.84- `--api-key`: Pass the API key directly.85- `--api-key-file`: Read the API key from a file.86- `--crop`: Apply `left,top,right,bottom` cropping after generation.87- `--resize`: Apply `WIDTHxHEIGHT` resizing after generation.88- `--format`: Force the saved format to `PNG`, `JPEG`, or `WEBP`.89- `--dry-run`: Print the resolved request payload without calling the API.9091## API Key Resolution9293Resolve credentials in this order:94951. `--api-key`962. `GEMINI_API_KEY`973. `GOOGLE_API_KEY`984. `--api-key-file`995. `GEMINI_API_KEY_FILE`1006. `GOOGLE_API_KEY_FILE`101102Accept these key file formats:103104- Plain text containing only the key105- `.env` style: `GEMINI_API_KEY=...`106- `.env` style: `GOOGLE_API_KEY=...`107108## When to Read References109110Read [references/gemini-image-generation.md](references/gemini-image-generation.md) when you need:111112- Current model names or their intended roles113- The latest documented SDK usage for generation vs image editing114- A reminder of installation, auth conventions, chat history, or multi-turn image workflows115116Read [references/prompting-guide.md](references/prompting-guide.md) when:117118- The model is producing vague, generic, or inconsistent results119- You need better generation prompts for a product shot, icon, poster, mockup, or hero image120- You need tighter edit prompts that clearly separate what should change from what should stay fixed121- You are doing multi-turn refinement and want prompts that reduce drift between turns122123## Constraints124125- Use the bundled script and the official Google GenAI SDK instead of raw REST calls.126- Prefer `--dry-run` first when verifying prompt assembly or key resolution.127- Treat `google-genai` and `Pillow` as required dependencies for this skill.128- Use `--input-image` whenever the request is an edit, restyle, or transformation of an existing image.129- Use `--state-file` for iterative refinement across separate CLI invocations.130- Use `--reset-state` when reusing a state-file path for a completely new concept.131- Keep prompts explicit about subject, composition, lighting, style, and text rendering requirements.132- Treat generated images as local outputs that should be saved to a user-specified path when one is available.133- Do not claim a generation succeeded unless the script actually saved image files.134135## Files136137- `scripts/generate_image.py`: SDK-based CLI for generation, editing, and light post-processing.138- `references/gemini-image-generation.md`: Current Google Gemini image generation notes.139- `references/prompting-guide.md`: Practical prompt-writing patterns for generation, editing, and multi-turn refinement.140141## Source142143- https://ai.google.dev/gemini-api/docs/image-generation