OpenAI 编辑图片
/v1/images/edits 是 OpenAI Images API 兼容的同步图片编辑接口。普通 API Key 走同步图片网关;任务 API Key 使用任务 API 的模型和价格配置,并同步等待编辑结果。
接口说明
| 项目 | 值 |
|---|---|
| Method | POST |
| Path | /v1/images/edits,也支持 /images/edits |
| Content-Type | multipart/form-data 或 application/json |
| 认证 | Authorization: Bearer YOUR_API_KEY |
| 普通 API Key | 需要 OpenAI 平台分组,且分组启用生图能力 |
| 任务 API Key | 使用任务 API 的模型和计费配置 |
使用任务 API Key 时,本接口最多同步等待 120 秒。如果等待超时且响应包含 task_id,请使用任务 API 查询最终状态。
Multipart 参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 否 | 图片模型,默认 gpt-image-2。普通 API Key 需使用 OpenAI Images 兼容模型;任务 API Key 可使用任务 API 模型列表中的图片模型。 |
prompt | string | 是 | 编辑指令。 |
image | file | 是 | 输入图片。多个图片可使用多个 image 字段或 image[]。 |
mask | file | 否 | 蒙版图片。 |
size | string | 否 | 输出尺寸。 |
n | integer | 否 | 生成图片数量。 |
response_format | string | 否 | 响应格式。 |
input_fidelity | string | 否 | OpenAI Images API 编辑参数,是否生效取决于所选模型。 |
JSON 参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 否 | 图片模型,默认 gpt-image-2。普通 API Key 需使用 OpenAI Images 兼容模型;任务 API Key 可使用任务 API 模型列表中的图片模型。 |
prompt | string | 是 | 编辑指令。 |
images | array | 是 | 每项支持 { "image_url": "https://..." }。file_id 当前不支持。 |
mask.image_url | string | 否 | 蒙版图片 URL。mask.file_id 当前不支持。 |
size | string | 否 | 输出尺寸。 |
n | integer | 否 | 生成图片数量。 |
使用 JSON 请求时,图片 URL 需要能被服务端访问;如果图片只在客户端本地,请使用 multipart 上传。
Multipart 示例
bash
curl https://api.llmapi.site/v1/images/edits \
-H 'Authorization: Bearer YOUR_API_KEY' \
-F 'model=gpt-image-2' \
-F 'image=@input.png' \
-F 'prompt=change the background to a clean studio setting' \
-F 'size=1024x1024'JSON 示例
bash
curl https://api.llmapi.site/v1/images/edits \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-image-2",
"prompt": "replace the background with a bright studio",
"images": [
{ "image_url": "https://example.com/input.png" }
],
"size": "1024x1024"
}'响应 — 200 OK
json
{
"created": 1779912000,
"data": [
{
"b64_json": "..."
}
]
}| 字段 | 说明 |
|---|---|
created | 响应创建时间,Unix 时间戳。 |
data[].b64_json | 当 response_format 为 b64_json 时返回的编辑后图片 Base64。 |
data[].url | 当 URL 响应可用时返回的编辑后图片地址。 |
错误码
| 状态码 | 原因 |
|---|---|
400 | 请求体无效、缺少图片文件或 images[].image_url,或使用了 file_id 等不支持字段。 |
401 | API Key 缺失或无效。 |
403 | 余额、额度或分组权限校验失败。 |
404 | 当前 API Key 分组不能调用 OpenAI Images API。 |
429 | 达到频率限制或并发限制。 |
504 | 任务 API Key 同步等待超时;如果后续可查询最终状态,响应会包含 task_id。 |
500 / 502 / 503 | 网关或上游服务商失败。 |