apiyi-gpt-image-2-gen
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese图片生成与编辑(GPT Image 2 官方正式版)
Image Generation and Editing (Official Formal Version of GPT Image 2)
基于API易平台的GPT Image 2模型(gpt-image-2)官方正式版实现图片生成技能,可以通过自然语言帮助用户生成图片,通过API易国内代理服务访问,支持Node.js和Python两种运行环境。gpt-image-2是API易平台的官方正式版GPT图像生成模型,支持精确的尺寸/画质控制(含4K),按token计费。
Based on the official formal version of the GPT Image 2 model (gpt-image-2) from Apiyi Platform, this image generation skill can help users generate images through natural language. It is accessed via Apiyi's domestic proxy service and supports both Node.js and Python runtime environments. gpt-image-2 is the official formal version of GPT image generation model on Apiyi Platform, supporting precise size/quality control (including 4K) and billed by token.
使用指引
Usage Guide
遵循以下步骤:
Follow these steps:
第1步:分析需求与参数提取
Step 1: Analyze Requirements and Extract Parameters
-
明确意图:区分用户是需要【文生图】(生成新图片)还是【图生图】(编辑/修改现有图片)或【多图融合】。
-
提示词(Prompt)分析:
- 使用用户原始完整输入:把用户输入的原始完整问题需求描述(原文)直接作为 提示词的主体,避免自行改写、总结或二次创作,防止细节丢失。
-p - 需要补充时先确认:如果信息不足(例如缺少风格、主体数量、镜头语言、场景细节、文字内容、禁止元素等),先向用户提问确认;用户确认后,再把补充内容以"追加"的方式拼接到原始提示词后。
- 样例:
- 用户输入:"帮我生成一张猫的图片,风格要可爱一点。"
- 正例说明:直接使用用户输入作为提示词:
-p "帮我生成一张猫的图片,风格要可爱一点。" - 反例说明:擅自改写为"生成一张可爱风格的猫的图片"会丢失用户原始输入的细节和语气。
- 如果需要补充细节(例如颜色、背景等),先提问确认:"你希望猫是什么颜色的?背景有什么要求吗?"用户回答后,再追加到提示词中:
-p "帮我生成一张猫的图片,风格要可爱一点。猫是橘色的,背景是草地。"
- 使用用户原始完整输入:把用户输入的原始完整问题需求描述(原文)直接作为
-
关键参数整理:
- Prompt(必需):提示词分析后的最终提示词(默认=用户原始完整且一致的输入;仅在用户确认后才追加补充信息)。
- Filename(可选):输出图片文件名/路径(需包含文件随机标识,避免重复)。不传则脚本会自动生成带时间戳的文件名。建议根据内容生成合理文件名(例如 ),避免使用通用名。
cat_in_garden.png - Size(可选):输出尺寸。
- 预设值:、
1024x1024、1536x1024、1024x1536、2048x2048、2048x1152、3840x21602160x3840 - 也可使用自定义尺寸(满足:最大边≤3840、两边16倍数、比例≤3:1、总像素0.65–8.3MP)
- 默认由模型自适应(auto)
- 预设值:
- Quality(可选):画质档位。(草图/批量)、
low(日常)、medium(终稿/精细文字)、high(默认)auto - Output Format(可选):(默认)、
png、jpegwebp - Output Compression(可选):输出压缩率(0-100),仅jpeg/webp生效
- 注意:该模型使用官方正式版端点,与官逆版gpt-image-2-all不同。
-
Clarify Intent: Distinguish whether the user needs [Text-to-Image] (generate new images), [Image-to-Image] (edit/modify existing images), or [Multi-Image Fusion].
-
Prompt Analysis:
- Use the user's original complete input: Directly use the user's original full question and requirement description as the main body of the prompt. Avoid rewriting, summarizing or secondary creation on your own to prevent loss of details.
-p - Confirm first when supplementation is needed: If information is insufficient (e.g., missing style, number of subjects, shot language, scene details, text content, prohibited elements, etc.), ask the user for confirmation first; after the user confirms, append the supplementary content to the original prompt in an "appending" manner.
- Examples:
- User input: "Help me generate a picture of a cat, in a cute style."
- Correct example: Use the user's input directly as the prompt:
-p "Help me generate a picture of a cat, in a cute style." - Incorrect example: Unauthorized rewriting to "Generate a picture of a cat in cute style" will lose the details and tone of the user's original input.
- If additional details are needed (e.g., color, background, etc.), ask for confirmation first: "What color do you want the cat to be? Any requirements for the background?" After the user replies, append it to the prompt:
-p "Help me generate a picture of a cat, in a cute style. The cat is orange, and the background is grass."
- Use the user's original complete input: Directly use the user's original full question and requirement description as the main body of the
-
Key Parameter Organization:
- Prompt (Required): The final prompt after analysis (default = the user's original complete and consistent input; only append supplementary information 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 content (e.g., ) instead of using generic names.
cat_in_garden.png - Size (Optional): Output size.
- Preset values: ,
1024x1024,1536x1024,1024x1536,2048x2048,2048x1152,3840x21602160x3840 - Custom sizes are also allowed (requirements: maximum side ≤3840, both sides are multiples of 16, aspect ratio ≤3:1, total pixels 0.65–8.3MP)
- Default is model-adaptive (auto)
- Preset values:
- Quality (Optional): Quality level. (sketch/batch),
low(daily use),medium(final draft/fine text),high(default)auto - Output Format (Optional): (default),
png,jpegwebp - Output Compression (Optional): Output compression rate (0-100), only valid for jpeg/webp
- Note: This model uses official formal endpoints, which are different from the reverse version gpt-image-2-all.
第2步:环境检查与命令执行
Step 2: Environment Check and Command Execution
-
检查环境:确认环境变量是否已设置(通常假定已设置,若运行失败再提示用户)���
APIYI_API_KEY -
构建并运行命令:
- 优先尝试 Node.js 版本:如果环境有 Node(命令可用),优先使用
node(零依赖,参数与 Python 保持一致)。scripts/generate_image.js - Node 不可用再用 Python 版本:使用 。
scripts/generate_image.py
文生图命令模板(优先 Node.js):bashnode scripts/generate_image.js -p "{prompt}" -f "{filename}" [-s {size}] [-q {quality}] [-o {output_format}]图生图命令模板(优先 Node.js):bashnode scripts/generate_image.js -p "{edit_instruction}" -i "{input_path}" -f "{output_filename}" [-s {size}] [-q {quality}]多图融合命令模板(优先 Node.js):bashnode scripts/generate_image.js -p "融合图1和图2的风格" -i ref1.png ref2.png -f "merged.png" [-s {size}] [-q {quality}](可选)Python 版本命令模板(Node 不可用时):bashpython 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}] - 优先尝试 Node.js 版本:如果环境有 Node(
-
Check Environment: Confirm whether theenvironment variable is set (usually assumed to be set; prompt the user if execution fails).
APIYI_API_KEY -
Build and Run Commands:
- Priority Node.js Version: If Node is available in the environment (the command works), prefer using
node(zero dependencies, parameters are consistent with Python).scripts/generate_image.js - Use Python Version if Node is Unavailable: Use .
scripts/generate_image.py
Text-to-Image Command Template (Priority Node.js):bashnode scripts/generate_image.js -p "{prompt}" -f "{filename}" [-s {size}] [-q {quality}] [-o {output_format}]Image-to-Image Command Template (Priority Node.js):bashnode scripts/generate_image.js -p "{edit_instruction}" -i "{input_path}" -f "{output_filename}" [-s {size}] [-q {quality}]Multi-Image Fusion Command Template (Priority Node.js):bashnode scripts/generate_image.js -p "Merge the styles of Image 1 and Image 2" -i ref1.png ref2.png -f "merged.png" [-s {size}] [-q {quality}](Optional) Python Version Command Template (When Node is Unavailable):bashpython 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}] - Priority Node.js Version: If Node is available in the environment (the
⏱️ 长时间任务处理策略
⏱️ Long-running Task Processing Strategy
1. 任务前提示
1. Pre-task Prompt
执行前必须告知用户:
- "图片生成已启动,预计需要120-150秒,请耐心等待"
Must inform the user before execution:
- "Image generation has started, it is expected to take 120-150 seconds, please wait patiently"
2. 🎨 最佳实践示例
2. 🎨 Best Practice Example
"图片生成中,预计120-150秒完成...\n⏳ 正在生成...\n(high + 2K/4K 复杂场景可能需要更长时间,请耐心等待)"
"Image generation in progress, expected to complete in 120-150 seconds...\n⏳ Generating...\n(Complex scenes with high quality + 2K/4K may take longer, please wait patiently)"
第3步:结果反馈
Step 3: Result Feedback
- 执行反馈:等待终端命令执行完毕。
- 成功:告知用户图片已生成,并指出保存路径。
- 失败:
- 若提示 API Key 缺失,请指导用户设置环境变量。
- 若提示网络错误,建议用户检查网络或稍后重试。
- Execution Feedback: Wait for the terminal command to complete execution.
- Success: Inform the user that the image has been generated and indicate the save path.
- Failure:
- If prompted for missing API Key, guide the user to set the environment variable.
- If prompted for network error, suggest the user check the network or try again later.
命令行使用样例
Command Line Usage Examples
生成新图片
Generate New Images
bash
python scripts/generate_image.py -p "图片描述文本" -f "output.png" [-s {size}] [-q {quality}] [-o {output_format}]示例:
bash
undefinedbash
python scripts/generate_image.py -p "Image description text" -f "output.png" [-s {size}] [-q {quality}] [-o {output_format}]Example:
bash
undefined基础生成
Basic generation
python scripts/generate_image.py -p "一只可爱的橘猫在草地上玩耍" -f "cat.png"
python scripts/generate_image.py -p "A cute orange cat playing on the grass" -f "cat.png"
指定尺寸和画质
Specify size and quality
python scripts/generate_image.py -p "日落山脉风景" -f "sunset.png" -s "2048x1152" -q "high"
python scripts/generate_image.py -p "Sunset mountain landscape" -f "sunset.png" -s "2048x1152" -q "high"
竖版高清图片(适合手机壁纸)
Vertical HD image (suitable for mobile wallpaper)
python scripts/generate_image.py -p "城市夜景" -f "city.png" -s "2160x3840" -q "high"
python scripts/generate_image.py -p "City night view" -f "city.png" -s "2160x3840" -q "high"
输出为JPEG
Output as JPEG
python scripts/generate_image.py -p "风景照片" -f "landscape.jpg" -s "3840x2160" -q "high" -o "jpeg"
**(可选)Node.js 版本示例:**
```bashpython scripts/generate_image.py -p "Landscape photo" -f "landscape.jpg" -s "3840x2160" -q "high" -o "jpeg"
**(Optional) Node.js Version Example:**
```bash基础生成
Basic generation
node scripts/generate_image.js -p "一只可爱的橘猫在草地上玩耍" -f "cat.png"
node scripts/generate_image.js -p "A cute orange cat playing on the grass" -f "cat.png"
指定尺寸和画质
Specify size and quality
node scripts/generate_image.js -p "日落山脉风景" -f "sunset.png" -s "2048x1152" -q "high"
undefinednode scripts/generate_image.js -p "Sunset mountain landscape" -f "sunset.png" -s "2048x1152" -q "high"
undefined编辑已有图片
Edit Existing Images
bash
python scripts/generate_image.py -p "编辑指令" -f "output.png" -i "path/to/input.png" [-s {size}] [-q {quality}]示例:
bash
undefinedbash
python scripts/generate_image.py -p "Editing instruction" -f "output.png" -i "path/to/input.png" [-s {size}] [-q {quality}]Example:
bash
undefined修改风格
Modify style
python scripts/generate_image.py -p "将图片转换成水彩画风格" -f "watercolor.png" -i "original.png"
python scripts/generate_image.py -p "Convert the image to watercolor style" -f "watercolor.png" -i "original.png"
添加元素
Add elements
python scripts/generate_image.py -p "在天空添加彩虹" -f "rainbow.png" -i "landscape.png" -q "high"
python scripts/generate_image.py -p "Add a rainbow to the sky" -f "rainbow.png" -i "landscape.png" -q "high"
替换背景
Replace background
python scripts/generate_image.py -p "将背景换成海滩" -f "beach-bg.png" -i "portrait.png" -s "2048x2048"
**(可选)Node.js 版本示例:**
```bashpython scripts/generate_image.py -p "Change the background to a beach" -f "beach-bg.png" -i "portrait.png" -s "2048x2048"
**(Optional) Node.js Version Example:**
```bash修改风格
Modify style
node scripts/generate_image.js -p "将图片转换成水彩画风格" -f "watercolor.png" -i "original.png"
node scripts/generate_image.js -p "Convert the image to watercolor style" -f "watercolor.png" -i "original.png"
多图参考图融合(最多5张)
Multi-reference image fusion (up to 5 images)
node scripts/generate_image.js -p "把图1的人物放进图2的场景" -i ref1.png ref2.png -f "merged.png"
## 命令行参数说明
> Python 与 Node.js 版本参数保持一致(短参数与长参数等价)。
| 参数 | 必填 | 说明 |
|------|------|------|
| `-p` / `--prompt` | 是 | 图片描述(文生图)或编辑指令(图生图)。保留用户原始完整输入。 |
| `-f` / `--filename` | 否 | 输出图片路径/文件名;不传则自动生成带时间戳的文件名。 |
| `-s` / `--size` | 否 | 输出尺寸:1024x1024 / 1536x1024 / 1024x1536 / 2048x2048 / 2048x1152 / 3840x2160 / 2160x3840 或自定义尺寸。 |
| `-q` / `--quality` | 否 | 画质档位:low / medium / high / auto(默认auto)。 |
| `-o` / `--output-format` | 否 | 输出格式:png(默认)/ jpeg / webp。 |
| `-c` / `--output-compression` | 否 | 输出压缩率(0-100),仅jpeg/webp生效。 |
| `-i` / `--input-image` | 否 | 图生图输入图片路径;可传多张(最多5张)。传入该参数即进入编辑模式。 |node scripts/generate_image.js -p "Put the character from Image 1 into the scene of Image 2" -i ref1.png ref2.png -f "merged.png"
## Command Line Parameter Description
> Parameters are consistent between Python and Node.js versions (short parameters are equivalent to long parameters).
| Parameter | Required | Description |
|------|------|------|
| `-p` / `--prompt` | Yes | Image description (text-to-image) or editing instruction (image-to-image). Retain the user's original complete input. |
| `-f` / `--filename` | No | Output image path/filename; if not provided, a filename with timestamp will be generated automatically. |
| `-s` / `--size` | No | Output size: 1024x1024 / 1536x1024 / 1024x1536 / 2048x2048 / 2048x1152 / 3840x2160 / 2160x3840 or 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 valid 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
| 资源 | 说明 |
|---|---|
| Node.js 版本(零依赖,优先使用) |
| Python 版本(备选) |
| 尺寸与比例控制文档,需要时使用,按需加载 |
| 批量生成配置模板,需要批量生成时使用,按需加载 |
| Resource | Description |
|---|---|
| Node.js version (zero dependencies, prefer to use) |
| Python version (alternative) |
| Size and aspect ratio control document, use when needed, load on demand |
| Batch generation configuration template, use when batch generation is needed, load on demand |
批量生成
Batch Generation
当用户需要一次性生成多张图片(批量生成)时:
- 加载配置模板:references/batch-template.md — 包含 JSON 配置格式说明和使用示例
- 获取/生成 JSON 文件:用户可自行提供 JSON 文件,或描述需求后 AI 根据需求生成
- 逐个执行:读取 prompts 数组,逐个执行生成命令,每张完成后反馈结果
- 汇总反馈:完成后告知成功数量、图片路径列表
注意:批量任务总时间 = 单张时间(120-150秒) × 图片数量,请提前告知用户预估时长。
When users need 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: Users can provide their own JSON file, or describe requirements and let AI generate it based on the requirements
- Execute One by One: Read the prompts array, execute the generation command one by one, and feedback the result after each image is completed
- Summary Feedback: After completion, inform the user of the number of successful images and the list of image paths
Note: Total time for batch tasks = single image time (120-150 seconds) × number of images, please inform the user of the estimated duration in advance.
画质说明
Quality Description
| 画质 | 说明 | 适用场景 |
|---|---|---|
| low | 草图/批量生成 | 快速预览、多次迭代 |
| medium | 日常 | 普通使用 |
| high | 终稿/精细文字 | 最终输出、包含文字的图像 |
| auto | 默认 | 由模型决定 |
| Quality | Description | Applicable Scenario |
|---|---|---|
| low | Sketch/batch generation | Quick preview, multiple iterations |
| medium | Daily use | General usage |
| high | Final draft/fine text | Final output, images containing text |
| auto | Default | Determined by the model |
输出格式说明
Output Format Description
| 格式 | 说明 | 适用场景 |
|---|---|---|
| png | 无压缩,透明背景 | 需要透明背景、保留最佳画质 |
| jpeg | 有压缩 | 照片、存储空间敏感 |
| webp | 现代格式 | Web使用、平衡画质与大小 |
注意:b64_json字段是纯base64,不含 前缀。客户端需要:
data:image/...;base64,- 写文件:→ 写入磁盘
base64.b64decode(b64_str) - 浏览器渲染:自行拼前缀 + b64
data:image/png;base64,
| Format | Description | Applicable Scenario |
|---|---|---|
| png | Lossless compression, transparent background | Need transparent background, retain best quality |
| jpeg | Compressed | Photos, storage space sensitive |
| webp | Modern format | Web usage, balance quality and size |
Note: The b64_json field is pure base64, without the prefix. Clients need to:
data:image/...;base64,- Write to file: → write to disk
base64.b64decode(b64_str) - Render in browser: Append the prefix + b64 on your own
data:image/png;base64,
注意事项
Notes
- API密钥必须设置,可通过环境变量或命令行参数提供
- 图片生成时间:约120-150秒,high + 2K/4K 复杂场景可能需要更长时间
- 编辑图片时,使用multipart/form-data上传参考图
- 确保输出目录有写入权限
- 按token计费(非按张)
- API Key must be set, can be provided via environment variable or command line parameter
- Image generation time: Approximately 120-150 seconds, complex scenes with high quality + 2K/4K may take longer
- When editing images, use multipart/form-data to upload reference images
- Ensure the output directory has write permission
- Billed by token (not per image)
API Key设置与获取
API Key Setup and Acquisition
如何获取API Key
How to Obtain API Key
如果你还没有API密钥,请前往 https://api.apiyi.com 注册账号并申请API Key。
获取步骤:
- 访问 https://api.apiyi.com
- 注册/登录你的账号
- 在控制台中创建API密钥
- 复制密钥并设置环境变量或在命令行中使用
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.
Acquisition steps:
- Visit https://api.apiyi.com
- Register/login your account
- Create an API Key in the console
- Copy the key and set the environment variable or use it in the command line
设置API Key
Set API Key
脚本从环境变量 获取API密钥。
APIYI_API_KEY设置环境变量:
bash
undefinedThe script obtains the API Key from the environment variable .
APIYI_API_KEYSet Environment Variable:
bash
undefinedLinux/Mac
Linux/Mac
export APIYI_API_KEY="your-api-key-here"
我的电脑高级设置中设置环境变量或者执行set 命令:
export APIYI_API_KEY="your-api-key-here"
Set environment variable in advanced settings of your computer or execute the set command:
Windows CMD
Windows CMD
set APIYI_API_KEY=your-api-key-here
set APIYI_API_KEY=your-api-key-here
Windows PowerShell
Windows PowerShell
$env:APIYI_API_KEY="your-api-key-here"
undefined$env:APIYI_API_KEY="your-api-key-here"
undefinedAPI端点说明
API Endpoint Description
文生图端点:POST /v1/images/generations
Text-to-Image Endpoint: POST /v1/images/generations
文生图端点,使用JSON格式请求。
Text-to-image endpoint, uses JSON format for requests.
图生图端点:POST /v1/images/edits
Image-to-Image Endpoint: POST /v1/images/edits
图生图端点,使用multipart/form-data格式请求。上传参考图(最多5张)+ 指令进行单图改图、多图融合。
参考图顺序有意义,prompt中可用"图1/图2/图3"指代。
Image-to-image endpoint, uses multipart/form-data format for requests. Upload reference images (up to 5) + instructions for single image editing and multi-image fusion.
The order of reference images is meaningful, and "Image 1/Image 2/Image 3" can be used in the prompt to refer to them.
模型信息
Model Information
- 模型名:gpt-image-2
- 出图速度:约 120-150秒(4K复杂场景可能需要更长时间)
- 输出分辨率:1024x1024 / 1536x1024 / 1024x1536 / 2048x2048 / 2048x1152 / 3840x2160 / 2160x3840 或自定义
- 默认响应格式:b64_json(纯base64,无前缀)
- 画质档位:low / medium / high / auto
- 输出格式:png / jpeg / webp
- 支持能力:文生图、单图编辑、多图融合
- 计费方式:按token计费
- Model Name: gpt-image-2
- Image Generation Speed: Approximately 120-150 seconds (4K complex 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 Levels: low / medium / high / auto
- Output Formats: png / jpeg / webp
- Supported Capabilities: Text-to-image, single image editing, multi-image fusion
- Billing Method: Billed by token
gpt-image-2(官转)vs gpt-image-2-all(官逆)对比
Comparison between gpt-image-2 (Official Formal Version) vs gpt-image-2-all (Official Reverse Version)
| 特性 | gpt-image-2 | gpt-image-2-all |
|---|---|---|
| 性质 | 官方正式版 | 官方逆向版 |
| 计费 | 按token | 统一$0.03/张 |
| 端点 | /v1/images/generations, /v1/images/edits | /v1/chat/completions |
| 上传参考图 | multipart form-data | base64 data URL |
| 下载图片 | b64_json(纯base64) | url或b64_json(带前缀) |
| 多图融合 | image[]数组最多5张 | chat多个image_url |
| 尺寸控制 | 显式size参数 | prompt描述 |
| 速度 | 约120-150秒 | 约60-300秒 |
| Feature | gpt-image-2 | gpt-image-2-all |
|---|---|---|
| Nature | Official formal version | Official reverse version |
| Billing | Billed by token | Fixed $0.03 per image |
| Endpoints | /v1/images/generations, /v1/images/edits | /v1/chat/completions |
| Reference Image Upload | multipart form-data | base64 data URL |
| Image Download | 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 | Approximately 120-150 seconds | Approximately 60-300 seconds |
作者介绍
Author Introduction
- 爱海贼的无处不在
- 我的微信公众号:无处不在的技术
- LoveOnePiece_Ubiquitous
- My WeChat Official Account: Ubiquitous Technology