An image generation skill implemented based on Google's Gemini 3.1 Flash model, which can help users generate images through natural language, accessed via API易's domestic proxy service, and supports both Node.js and Python runtime environments. Nano Banana 2 (codename) is Google's latest image generation model released on February 26, 2026, with the model ID gemini-3.1-flash-image-preview. It redefines the cost-effectiveness of image generation with Pro-level image quality + Flash-level speed and cost, and is the latest flagship in the Nano Banana series.
Usage Guidelines
Follow these steps:
Step 1: Analyze requirements and extract parameters
- Clarify intent: Distinguish whether the user needs [text-to-image] (generate a new image) or [image-to-image] (edit/modify an existing image).
- Prompt analysis:
- Use the user's original complete input: Directly use the user's original complete problem/requirement description (original text) as the
-pmain body of the prompt, avoiding rewriting, summarizing, or secondary creation on your own to prevent loss of details. - Confirm first when supplementation is needed: If the information is insufficient (for example, missing style, number of subjects, camera language, scene details, text content, prohibited elements, etc.), first ask the user to confirm; after the user confirms, then append the supplementary contentin an "append" mannerafter the original prompt.
- Example:
- User input: "Help me generate an image of a cat, and make the style a bit cute."
- Positive example: Directly use the user's input as the prompt:
-p "帮我生成一张猫的图片,风格要可爱一点。" - Negative example: Rewriting it on your own as "Generate a cute-style image of a cat" will lose the details and tone of the user's original input.
- If details need to be supplemented (for example, color, background, etc.), first ask and confirm: "What color do you want the cat to be? Are there any requirements for the background?" After the user answers, then append it to the prompt:
-p "帮我生成一张猫的图片,风格要可爱一点。猫是橘色的,背景是草地。"
- Use the user's original complete input: Directly use the user's original complete problem/requirement description (original text) as the
- Organize key parameters:
- Prompt (required): The final prompt after prompt analysis (default = the user's original complete and consistent input; supplementary information is appended only after user confirmation).
- Filename (optional): Output image filename/path (must include a random file identifier to avoid duplication). If not provided, the script will automatically generate a filename with a timestamp. It is recommended to generate a reasonable filename based on the content (for example,
cat_in_garden.png), avoiding generic names. - Aspect Ratio (optional): Infer the ratio based on the user's description. For example:
- "mobile wallpaper" ->
9:16 - "computer wallpaper/video cover" ->
16:9 - "avatar" ->
1:1 - By default, if the user does not explicitly specify an image ratio, keep the image ratio empty.
- "mobile wallpaper" ->
- Resolution (optional):
- By default, the image ratio uses
2K。 - Use only for extreme high-definition needs or when specified by the user
4K, and through a friendly prompt, remind the user that generation is slower and to wait patiently. - Note: Parameter values must be uppercase (
1K,2K,4K)。
- By default, the image ratio uses
Step 2: Environment Check and Command Execution
-
Check environment: Confirm whether the
APIYI_API_KEYenvironment variable is set (usually assumed to be set; if execution fails, prompt the user). -
Build and run the command:
- Try the Node.js version first: If the environment has Node (the
nodecommand is available), prefer usingscripts/generate_image.js(zero dependencies, parameters kept consistent with Python). - If Node is unavailable, use the Python version: Use
scripts/generate_image.py。
Text-to-image command template (prefer Node.js):
node scripts/generate_image.js -p "{prompt}" -f "{filename}" [-a {ratio}] [-r {res}]Image-to-image command template (prefer Node.js):
node scripts/generate_image.js -p "{edit_instruction}" -i "{input_path}" -f "{output_filename}" [-r {res}](Optional) Python version command template (when Node is unavailable):
python scripts/generate_image.py -p "{prompt}" -f "{filename}" [-a {ratio}] [-r {res}] python scripts/generate_image.py -p "{edit_instruction}" -i "{input_path}" -f "{output_filename}" [-r {res}] - Try the Node.js version first: If the environment has Node (the
⏱️ Long-running task handling strategy
1. Pre-task notification
Must inform the user before execution:
- "Image generation has started, expected to take 25 seconds to 5 minutes"
2. 🎨 Best practice examples
- Quick generation scenario (1K resolution)
"Quick mode: 1K resolution generation, expected to complete within 30 seconds"
- High-quality generation scenario (2K/4K resolution)
"High-quality mode: 2K resolution generation, expected to take 1-4 minutes\n⏳ Starting generation... 🔄"
Step 3: Result feedback
- Execution feedback: Wait for the terminal command to finish executing.
- Success: Inform the user that the image has been generated and point out the save path.
- Failure:
- If it indicates the API Key is missing, guide the user to set the environment variable.
- If it indicates a network error, suggest the user check the network or try again later.
Command-line usage examples
Generate a new image
python scripts/generate_image.py -p "图片描述文本" -f "output.png" [-a 1:1] [-r 1K]
Example:
# 基础生成
python scripts/generate_image.py -p "一只可爱的橘猫在草地上玩耍" -f "cat.png"
# 指定比例和分辨率
python scripts/generate_image.py -p "日落山脉风景" -f "sunset.png" -a 16:9 -r 4K
# 竖版高清图片(适合手机壁纸)
python scripts/generate_image.py -p "城市夜景" -f "city.png" -a 9:16 -r 2K
(Optional) Node.js version example:
# 基础生成
node scripts/generate_image.js -p "一只可爱的橘猫在草地上玩耍" -f "cat.png"
# 指定比例和分辨率
node scripts/generate_image.js -p "日落山脉风景" -f "sunset.png" -a 16:9 -r 4K
Edit an existing image
python scripts/generate_image.py -p "编辑指令" -f "output.png" -i "path/to/input.png" [-a 1:1] [-r 1K]
Example:
# 修改风格
python scripts/generate_image.py -p "将图片转换成水彩画风格" -f "watercolor.png" -i "original.png"
# 添加元素
python scripts/generate_image.py -p "在天空添加彩虹" -f "rainbow.png" -i "landscape.png" -r 2K
# 替换背景
python scripts/generate_image.py -p "将背景换成海滩" -f "beach-bg.png" -i "portrait.png" -a 3:4
(Optional) Node.js version example:
# 修改风格
node scripts/generate_image.js -p "将图片转换成水彩画风格" -f "watercolor.png" -i "original.png"
# 多张参考图(最多14张)
node scripts/generate_image.js -p "参考多张图片融合风格" -i ref1.png ref2.png ref3.png -f "merged.png"
Additional resources
- Common usage scenario documentation: references/scene.md
Command-line parameter description
The Python and Node.js versions keep consistent parameters (short parameters are equivalent to long parameters).
| Parameter | Required | Description |
|---|---|---|
-p / --prompt |
Yes | Image description (text-to-image) or editing instruction (image-to-image). Preserve the user's original complete input. |
-f / --filename |
No | Output image path/filename; if not provided, automatically generate a PNG filename with a timestamp and write it to the current directory. |
-a / --aspect-ratio |
No | Image aspect ratio:1:1、16:9、9:16、4:3、3:4、3:2、2:3、5:4、4:5、1:4、4:1、1:8、8:1、21:9。 |
-r / --resolution |
No | Image resolution:1K / 2K / 4K(must be uppercase). If not provided, it is not specified in the request and is decided by the API side. |
-i / --input-image |
No | Input image path for image-to-image; multiple images can be provided (up to 14). Passing this parameter enters editing mode. |
Image parameter description
aspect_ratio - Image aspect ratio
The following 14 aspect ratios are supported:
| Aspect ratio | Orientation | Applicable scenarios |
|---|---|---|
| 1:1 | Square | Avatars, Instagram posts |
| 16:9 | Landscape | YouTube thumbnails, desktop wallpapers, presentations |
| 9:16 | Portrait | Douyin/TikTok, Instagram Stories, phone wallpapers |
| 4:3 | Landscape | Classic photos, presentations |
| 3:4 | Portrait | Pinterest, portrait photography |
| 3:2 | Landscape | DSLR camera standard, print media |
| 2:3 | Portrait | Portrait posters |
| 5:4 | Landscape | Large-format printing, art prints |
| 4:5 | Portrait | Instagram posts, social media |
| 21:9 | Ultra-wide | Cinematic, banners, panoramas |
| 1:4 | Ultra-tall | Phone long images, comics |
| 4:1 | Ultra-wide | Banners, website headers |
| 1:8 | Ultra-tall | Phone long images, comics |
| 8:1 | Ultra-wide | Banners, website headers |
resolution - image resolution
Three resolution options: 1K, 2K, 4K Note: Resolution values must be uppercase (1K, 2K, 4K) Default: 2K
Notes
- The API key must be set, and can be provided via environment variable or command-line argument
- Resolution parameters must be uppercase (1K/2K/4K); lowercase will default to 1K
- Image generation time: 25 seconds to 5 minutes, depending on resolution and server load
- When editing images, the input image is automatically converted to base64 encoding
- Ensure the output directory has write permissions
API Key Setup and Acquisition
How to Get an API Key
If you don't have an API key yet, please go to https://api.apiyi.com to register an account and apply for an API Key.
Steps to obtain:
- Visit https://api.apiyi.com
- Register/log in to your account
- Create an API key in the console
- Copy the key and set it as an environment variable or use it in the command line
Set API Key
The script looks for the API key in the following order:
--api-keyCommand-line argument (temporary use)APIYI_API_KEYEnvironment variable (recommended)
Set environment variable (recommended):
# Linux/Mac
export APIYI_API_KEY="your-api-key-here"
# Windows CMD
我的电脑高级设置中设置环境变量或者执行set APIYI_API_KEY=your-api-key-here
# Windows PowerShell
在我的电脑中设置环境变量:$env:APIYI_API_KEY="your-api-key-here"
Command-line argument method (temporary):
python scripts/generate_image.py -p "一只猫" -k "your-api-key-here"
About the Author
- The omnipresence of those who love pirates
- My WeChat official account: 无处不在的技术