gpt-image-2 API
本文介绍 gpt-image-2 模型调用 API 的输入输出参数,供您使用接口时查阅字段含义。
gpt-image-2 是 gpt-image-1.5 的升级版,支持文生图、参考图编辑、多图融合和 mask 局部重绘。相比 gpt-image-1.5 ,该模型支持更灵活的输出尺寸,最高可生成 4K 图像;图片编辑场景会自动启用高保真参考图处理,不再支持 input_fidelity 参数。
请求参数
文生图请求体
POST https://airouter.xincache.cn/v1/images/generations
字段名 | 类型 | 是否必须 | 默认值 | 描述 |
|---|
model | string | 必须 | - | 本次请求使用的模型名称,此处为 gpt-image-2。 |
prompt | string | 必须 | - | 提示词,支持中文和英文。 |
size | string | 可选 | auto
| 输出图片尺寸,支持预设尺寸或满足约束的自定义尺寸,详见尺寸说明。 |
quality | string | 可选 | auto
| 图片质量,支持 auto、 low、 medium、 high ;质量越高耗时通常越长。 |
output_format | string | 可选 | png
| 输出图片格式,支持 png、 jpeg、 webp。 |
output_compression | int | 可选 | - | 图片压缩强度,取值 0~100,仅对 jpeg 和 webp 生效。 |
background | string | 可选 | auto
| 背景模式,支持 auto、 opaque ;不支持 transparent。 |
moderation | string | 可选 | auto
| 内容审核强度,支持 auto、 low。 |
n | int | 可选 | 1 | 生成图片数量。 gpt-image-2 当前仅支持 1 ;如需多张图片,请发起多次请求。 |
图片编辑请求体
POST https://airouter.xincache.cn/v1/images/edits
使用 multipart/form-data 传参。
字段名 | 类型 | 是否必须 | 默认值 | 描述 |
|---|
model | string | 必须 | - | 本次请求使用的模型名称,此处为 gpt-image-2。 |
prompt | string | 必须 | - | 编辑或融合指令。多图融合时,可在提示词中使用“图1 / 图2 / 图3”指代上传顺序。 |
image[] | file[] | 必须 | - | 参考图,可重复传入,最多 5 张。建议单张不超过 10MB,支持 PNG、JPG、WebP。 |
mask | file | 可选 | - | 局部重绘蒙版,仅作用于第一张 image[] 。要求带 alpha 通道,透明区域为重绘区域。 |
size | string | 可选 | auto
| 输出图片尺寸,与文生图接口一致。 |
quality | string | 可选 | auto
| 图片质量,支持 auto、 low、 medium、 high。 |
output_format | string | 可选 | png
| 输出图片格式,支持 png、 jpeg、 webp。 |
output_compression | int/string | 可选 | - | 图片压缩强度,取值 0~100,仅对 jpeg 和 webp 生效。 |
background | string | 可选 | auto
| 背景模式,支持 auto、 opaque ;不支持 transparent。 |
尺寸说明
预设尺寸
size | 含义 | 像素级别 |
|---|
auto
| 自适应 | 模型决定 |
1024x1024
| 方形 1:1 | 1K |
1536x1024
| 横版 3:2 | 1K |
1024x1536
| 竖版 2:3 | 1K |
2048x2048
| 方形 1:1 | 2K |
2048x1152
| 横版 16:9 | 2K |
3840x2160
| 横版 16:9 | 4K |
2160x3840
| 竖版 9:16 | 4K |
自定义尺寸约束
gpt-image-2 支持满足以下条件的自定义尺寸:
最大边不超过 3840px。
宽和高都必须是 16 的倍数。
长边与短边比例不超过 3:1。
总像素数范围为 655,360 ~ 8,294,400。
合法示例: 1600x1200、 1792x1024、 2048x1536、 3200x1800。
非法示例: 1000x1000 (不是 16 的倍数)、 4000x4000 (超过最大边限制)、 3840x1000 (长短边比例超过 3:1)。
响应参数
字段名 | 类型 | 描述 |
|---|
created | integer
| 本次请求创建时间的 Unix 时间戳(秒)。 |
data | array
| 输出图像列表。 |
data[].b64_json | string
| Base64 编码的图片内容。 gpt-image-2 返回纯 Base64 字符串,不含前缀。 |
usage | object
| token 使用情况。 |
usage.input_tokens | integer
| 输入 token 数。图片编辑请求会包含参考图转换后的输入 token。 |
usage.output_tokens | integer
| 输出图片 token 数。 |
usage.total_tokens | integer
| 本次请求总 token 数。 |
error | object
| 错误信息对象。 |
error.code | string
| 错误码。 |
error.message | string
| 错误提示信息。 |
error.param | string
| 请求 id 或相关参数。 |
示例
OPENAI 兼容接口
curl
curl --location 'https://airouter.xincache.cn/v1/images/generations' \
--header "Authorization: Bearer $XINNETMODEL_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2",
"prompt": "赛博朋克城市夜景,霓虹灯招牌特写,电影感构图",
"size": "2048x1152",
"quality": "high",
"output_format": "jpeg",
"output_compression": 85
}'
python
import base64
import os
from openai import OpenAI
client = OpenAI(
base_url="https://airouter.xincache.cn/v1",
api_key=os.getenv("XINNETMODEL_API_KEY", "YOUR_API_KEY"),
)
res = client.images.generate(
model="gpt-image-2",
prompt="赛博朋克城市夜景,霓虹灯招牌特写,电影感构图",
size="2048x1152",
quality="high",
output_format="jpeg",
output_compression=85,
)
image_b64 = res.data[0].b64_json
with open("image.jpg", "wb") as f:
f.write(base64.b64decode(image_b64))
print("Saved to image.jpg")
图片编辑
使用 multipart/form-data 上传参考图。多图融合时重复传入 image[] 字段,上传顺序对应提示词中的“图1 / 图2 / 图3”。
curl
curl --location 'https://airouter.xincache.cn/v1/images/edits' \
--header "Authorization: Bearer $XINNETMODEL_API_KEY" \
-F "model=gpt-image-2" \
-F "prompt=将图1中的人物放到图2的海边日落场景中,保持人物细节和自然光照" \
-F "image[]=@/path/to/person.png" \
-F "image[]=@/path/to/beach.png" \
-F "size=1536x1024" \
-F "quality=high" \
-F "output_format=png"
python
import base64
import os
from openai import OpenAI
client = OpenAI(
base_url="https://airouter.xincache.cn/v1",
api_key=os.getenv("XINNETMODEL_API_KEY", "YOUR_API_KEY"),
)
with open("person.png", "rb") as person, open("beach.png", "rb") as beach:
res = client.images.edit(
model="gpt-image-2",
image=[person, beach],
prompt="将图1中的人物放到图2的海边日落场景中,保持人物细节和自然光照",
size="1536x1024",
quality="high",
)
image_b64 = res.data[0].b64_json
with open("edit.png", "wb") as f:
f.write(base64.b64decode(image_b64))
print("Saved to edit.png")
mask 局部重绘
curl --location 'https://airouter.xincache.cn/v1/images/edits' \
--header "Authorization: Bearer $XINNETMODEL_API_KEY" \
-F "model=gpt-image-2" \
-F "prompt=只在透明蒙版区域添加一只红色风筝,其他区域保持不变" \
-F "image[]=@/path/to/original.png" \
-F "mask=@/path/to/mask.png" \
-F "size=1024x1024" \
-F "quality=high"
响应
{
"created": 1776832476,
"data": [
{
"b64_json": "{image_base64_string}"
}
],
"usage": {
"input_tokens": 42,
"output_tokens": 6240,
"total_tokens": 6282
}
}
{
"error": {
"message": "error_message",
"type": "error_type",
"param": "request_id",
"code": "error_code"
}
}
使用建议
预设尺寸通常更稳定;只有确实需要特殊比例时再使用自定义尺寸。
草稿或批量场景可使用 quality=low ,默认成品可使用 medium ,文字、海报、精细纹理、印刷场景建议使用 high。
最终展示图片可优先使用 output_format=jpeg 与 output_compression=85 ,体积通常小于 PNG。
高画质 2K/4K 请求耗时更长,客户端超时时间建议设置为 360 秒以上。
gpt-image-2 不支持透明背景;需要透明背景时请使用后处理或回退到支持透明背景的模型。
Last modified: 25 April 2026