Image generation skill based on the GPT Image 2 model (gpt-image-2) official release on the API易 platform, 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. gpt-image-2 is the official release GPT image generation model on the API易 platform, supporting precise size/quality control (including 4K), billed by token.
Usage guide
Follow these steps:
Step 1: Analyze requirements and extract parameters
- Clarify intent: distinguish whether the user needs [text-to-image] (generate a new image), [image-to-image] (edit/modify an existing image), or [multi-image fusion].
- Prompt analysis:
- Use the user's original complete input: use the user's original complete problem/requirement description (verbatim) directly as the main body of the
-pprompt, avoiding rewriting, summarizing, or secondary creation on your own, to prevent loss of details. - Confirm first when supplementation is needed: if 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, the style should be a bit cute."
- Positive example: directly use the user input as the prompt:
-p "帮我生成一张猫的图片,风格要可爱一点。" - Negative example: arbitrarily rewriting it as "generate an image of a cute-style 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? Any requirements for the background?" After the user answers, then append to the prompt:
-p "帮我生成一张猫的图片,风格要可爱一点。猫是橘色的,背景是草地。"
- Use the user's original complete input: use the user's original complete problem/requirement description (verbatim) directly as the main body of 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 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), and avoid using generic names. - Size (optional): output size.
- Preset values:
1024x1024、1536x1024、1024x1536、2048x2048、2048x1152、3840x2160、2160x3840 - Custom sizes can also be used (satisfying: longest side ≤3840, both sides multiples of 16, ratio ≤3:1, total pixels 0.65–8.3MP)
- Defaults to model adaptive (auto)
- Preset values:
- Quality (optional): quality level.
low(sketch/batch),medium(daily),high(final draft/fine text),auto(default) - Output Format (optional):
png(default),jpeg、webp - Output Compression (optional): output compression rate (0-100), only effective for jpeg/webp
- Note: this model uses the official stable endpoint, unlike the unofficial reverse-engineered gpt-image-2-all.
Step 2: Environment Check and Command Execution
-
Check the environment: confirm whether the
APIYI_API_KEYenvironment variable is set (usually assumed to be set; if execution fails, then prompt the user)��� -
Build and run the command:
- Try the Node.js version first: if Node is available in the environment (the
nodecommand is available), preferscripts/generate_image.js(zero dependencies, parameters 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}" [-s {size}] [-q {quality}] [-o {output_format}]Image-to-image command template (prefer Node.js):
node scripts/generate_image.js -p "{edit_instruction}" -i "{input_path}" -f "{output_filename}" [-s {size}] [-q {quality}]Multi-image fusion command template (prefer Node.js):
node scripts/generate_image.js -p "融合图1和图2的风格" -i ref1.png ref2.png -f "merged.png" [-s {size}] [-q {quality}](optional) Python version command template (when Node is unavailable):
python scripts/generate_image.py -p "{prompt}" -f "{filename}" [-s {size}] [-q {quality}] [-o {output_format}] python scripts/generate_image.py -p "{edit_instruction}" -i "{input_path}" -f "{output_filename}" [-s {size}] [-q {quality}] - Try the Node.js version first: if Node is available in the environment (the
⏱️ Long-running task handling strategy
1. Pre-task notice
Must inform the user before execution:
- "Image generation has started, it is expected to take 120-150 seconds, please wait patiently"
2. 🎨 Best practice example
"Image is being generated, expected to complete in 120-150 seconds...\n⏳ Generating...\n(high + 2K/4K complex scenes may take longer, please wait patiently)"
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 indicate 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" [-s {size}] [-q {quality}] [-o {output_format}]
Example:
# 基础生成
python scripts/generate_image.py -p "一只可爱的橘猫在草地上玩耍" -f "cat.png"
# 指定尺寸和画质
python scripts/generate_image.py -p "日落山脉风景" -f "sunset.png" -s "2048x1152" -q "high"
# 竖版高清图片(适合手机壁纸)
python scripts/generate_image.py -p "城市夜景" -f "city.png" -s "2160x3840" -q "high"
# 输出为JPEG
python scripts/generate_image.py -p "风景照片" -f "landscape.jpg" -s "3840x2160" -q "high" -o "jpeg"
(optional) Node.js version example:
# 基础生成
node scripts/generate_image.js -p "一只可爱的橘猫在草地上玩耍" -f "cat.png"
# 指定尺寸和画质
node scripts/generate_image.js -p "日落山脉风景" -f "sunset.png" -s "2048x1152" -q "high"
Edit an existing image
python scripts/generate_image.py -p "编辑指令" -f "output.png" -i "path/to/input.png" [-s {size}] [-q {quality}]
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" -q "high"
# 替换背景
python scripts/generate_image.py -p "将背景换成海滩" -f "beach-bg.png" -i "portrait.png" -s "2048x2048"
(optional) Node.js version example:
# 修改风格
node scripts/generate_image.js -p "将图片转换成水彩画风格" -f "watercolor.png" -i "original.png"
# 多图参考图融合(最多5张)
node scripts/generate_image.js -p "把图1的人物放进图2的场景" -i ref1.png ref2.png -f "merged.png"
Command-line parameter description
The parameters of the Python and Node.js versions are consistent (short parameters and long parameters are equivalent).
| 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, a filename with a timestamp is automatically generated. |
-s / --size |
No | Output size: 1024x1024 / 1536x1024 / 1024x1536 / 2048x2048 / 2048x1152 / 3840x2160 / 2160x3840 or a custom size. |
-q / --quality |
No | Quality level: low / medium / high / auto (default auto). |
-o / --output-format |
No | Output format: png (default) / jpeg / webp. |
-c / --output-compression |
No | Output compression rate (0-100), only effective for jpeg/webp. |
-i / --input-image |
No | Input image path for image-to-image; multiple images can be passed (up to 5). Passing this parameter enters edit mode. |
File Resource Description
| Resource | Description |
|---|---|
scripts/generate_image.js |
Node.js version (zero dependencies, preferred) |
scripts/generate_image.py |
Python version (alternative) |
references/size-guide.md |
Size and aspect ratio control documentation, use when needed, load on demand |
references/batch-template.md |
Batch generation configuration template, use when batch generation is needed, load on demand |
Batch generation
When the user needs to generate multiple images at once (batch generation):
- Load configuration template:references/batch-template.md — includes JSON configuration format description and usage examples
- Obtain/generate JSON file: the user can provide a JSON file themselves, or describe the requirements and the AI will generate it based on the requirements
- Execute one by one: read the prompts array, execute the generation command one by one, and report the result after each image is completed
- Summary feedback: after completion, inform the number of successes and the list of image paths
Note: total batch task time = single image time (120-150 seconds) × number of images, please inform the user of the estimated duration in advance.
Quality Description
| Quality | Description | Applicable scenarios |
|---|---|---|
| low | Sketch/batch generation | Quick preview, multiple iterations |
| medium | Daily | General use |
| high | Final draft/fine text | Final output, images containing text |
| auto | Default | Determined by the model |
Output Format Description
| Format | Description | Applicable scenarios |
|---|---|---|
| png | No compression, transparent background | Requires transparent background, retains best quality |
| jpeg | Compressed | Photos, storage space sensitive |
| webp | Modern format | Web use, balances quality and size |
Note: the b64_json field is pure base64, without the data:image/...;base64, prefix. The client needs to:
- Write file:
base64.b64decode(b64_str)→ write to disk - Browser rendering: prepend the prefix yourself
data:image/png;base64,+ b64
Precautions
- The API key must be set, and can be provided via environment variable or command line parameter
- Image generation time: about 120-150 seconds; high + 2K/4K complex scenes may take longer
- When editing images, use multipart/form-data to upload the reference image
- Ensure the output directory has write permission
- Billed by token (not by image)
API Key Setting and Acquisition
How to get the API Key
If you do not 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 the environment variable or use it in the command line
Set the API Key
The script obtains the API key from the environment variable APIYI_API_KEY .
Set the environment variable:
# Linux/Mac
export APIYI_API_KEY="your-api-key-here"
我的电脑高级设置中设置环境变量或者执行set 命令:
# Windows CMD
set APIYI_API_KEY=your-api-key-here
# Windows PowerShell
$env:APIYI_API_KEY="your-api-key-here"
API Endpoint Description
Text-to-image endpoint: POST /v1/images/generations
Text-to-image endpoint, uses JSON format for requests.
Image-to-image endpoint: POST /v1/images/edits
Image-to-image endpoint, requested using multipart/form-data format. Upload reference images (up to 5) + instructions for single-image editing and multi-image fusion.
The order of reference images matters; in the prompt, you can use "image 1/image 2/image 3" to refer to them.
Model information
- Model name: gpt-image-2
- Image generation speed: about 120-150 seconds (complex 4K scenes may take longer)
- Output resolution: 1024x1024 / 1536x1024 / 1024x1536 / 2048x2048 / 2048x1152 / 3840x2160 / 2160x3840 or custom
- Default response format: b64_json (pure base64, no prefix)
- Quality level: low / medium / high / auto
- Output format: png / jpeg / webp
- Supported capabilities: text-to-image, single-image editing, multi-image fusion
- Billing method: billed by token
gpt-image-2 (official relay) vs gpt-image-2-all (official reverse) comparison
| Feature | gpt-image-2 | gpt-image-2-all |
|---|---|---|
| Nature | Official standard version | Official reverse version |
| Billing | By token | Flat $0.03/image |
| Endpoint | /v1/images/generations, /v1/images/edits | /v1/chat/completions |
| Upload reference images | multipart form-data | base64 data URL |
| Download images | b64_json (pure base64) | url or b64_json (with prefix) |
| Multi-image fusion | image[] array up to 5 images | chat multiple image_url |
| Size control | Explicit size parameter | prompt description |
| Speed | About 120-150 seconds | About 60-300 seconds |
Author introduction
- The love for One Piece is everywhere
- My WeChat official account: 无处不在的技术