POST /v1/images/edits
鉴权: {'type': 'bearer', 'description': '通过 `Authorization: Bearer sk-xxx` 提供 API Key'}
基於現有圖片生成新圖 (圖生圖 / image-to-image). 跟 [`/v1/images/generations`](./openai-gpt-image) (文生圖) 不同, 本端點要求客戶以 `multipart/form-data` 上傳影像檔案 + 文本編輯指令. ## 協議要點 | 維度 | 文生圖 `/v1/images/generations` | 圖生圖 `/v1/images/edits` (本端點) | |---|---|---| | Content-Type | `application/json` | **`multipart/form-data`** | | 必填欄位 | `model` + `prompt` | `model` + `prompt` + **`image` (檔案)** | | 多圖 | — | `image[]` 重複欄位名 (部分模型支援) | | 區域性編輯 | — | `mask` 可選 (透明區域為編輯目標) | ## 模型範圍 僅適用於 `gpt-image-*` 系列與 `dall-e-2`. 其他廠商的圖生圖能力 (如 Seedream / Qwen-Image-Edit) 請走 [`/v1/images/generations`](./openai-gpt-image) 端點的 JSON 協議 (上游協議是單 endpoint 設計). ## SDK 示例 (OpenAI Python SDK) ```python from openai import OpenAI client = OpenAI(api_key="sk-xxx", base_url="https://api.router.ai/v1") # 單圖基礎 resp = client.images.edit( image=open("photo.png", "rb"), prompt="把背景換成賽博朋克霓虹街道", model="gpt-image-2", size="1024x1024", ) # 區域性編輯 (含 mask) resp = client.images.edit( image=open("portrait.png", "rb"), mask=open("mask.png", "rb"), # 蒙版透明區域 = 編輯目標 prompt="把蒙版區域替換為藍天白雲", model="gpt-image-2", ) ```
image | file (binary) | required | 原始影像檔案 (PNG / JPEG / WebP). 單張時欄位名為 `image`; 多張參考圖請重複使用欄位名 `image[]` (部分模型支援多圖輸入) |
model | string | required | 影像模型 ID (如 `gpt-image-2` (推薦) / `gpt-image-1` / `dall-e-2`) |
prompt | string | required | 編輯指令文本; 描述越具體生成品質越高 (支援中英文) |
mask | file (binary) | 蒙版影像 (PNG; 透明區域為編輯目標). 僅部分模型支援區域性編輯模式 | |
n | integer | 生成圖片數量 | |
size | string | 影像尺寸 (寬×高). i2i 端點 (`/v1/images/edits`) **不支援 2K / 4K**, 僅官方明文擋位: `gpt-image-1 / gpt-image-2` → `1024x1024` / `1024x1536` / `1536x1024`; `dall-e-2` → `256x256` / `512x512` / `1024x1024`. 需要 2K / 4K 高解析度請走 t2i 端點 `/v1/images/generations` (gpt-image-2 支援 freeform 自定義, 16 倍數 / ≤3840px / 長短比 ≤3:1) | |
quality | string | 品質檔位. `low/medium/high` 是原生三檔; `standard/hd` 是 DALL-E 相容 alias | |
response_format | string | 返回格式. `gpt-image-*` 始終返 `b64_json`; `url` 僅 DALL-E 系列生效 | |
output_format | string | 輸出影像編碼格式 (僅 `gpt-image-*` 支援) | |
background | string | 背景類型; `transparent` 需配合 `output_format=png` 或 `webp` | |
user | string | 客戶端可選傳入的終端使用者標識 (用於上游濫用檢測) |
200 — 成功返回編輯後的影像 (含 base64). usage.input_tokens_details.image_tokens 反映輸入影像消耗的視覺 token (跟文生圖差異點)400 — 請求參數錯誤 (mask 尺寸跟 image 不匹配 / 不支援的影像格式 / model 不支援 mask 等)402 — 餘額不足# 单图图生图
curl https://api.router.ai/v1/images/edits \
-H "Authorization: Bearer sk-xxx" \
-F image="@photo.png" \
-F model="gpt-image-2" \
-F prompt="把背景换成赛博朋克霓虹街道, 保留人物主体不变" \
-F size="1024x1024" \
-F quality="high" \
-F n=1
# 多图参考 (部分模型支持; image[] 重复字段名上传多张)
curl https://api.router.ai/v1/images/edits \
-H "Authorization: Bearer sk-xxx" \
-F "image[]=@photo1.png" \
-F "image[]=@photo2.png" \
-F model="gpt-image-2" \
-F prompt="把这两张照片的主体合并到一张图里, 保留各自风格"
# 局部编辑 (mask 蒙版, 透明区域 = 编辑目标)
curl https://api.router.ai/v1/images/edits \
-H "Authorization: Bearer sk-xxx" \
-F image="@portrait.png" \
-F mask="@mask.png" \
-F model="gpt-image-2" \
-F prompt="把蒙版区域替换为蓝天白云" \
-F size="1024x1024"