Image2 接入文档
这份文档描述 `ai.cozesf.cc` 当前可用的 Image2 文生图与图生图接入方式。调用入口走本站统一网关,鉴权、调度和计费都在站内完成。
POST
/v1/images/generations
/v1/images/edits
1K 生图 / 图生图
0.2 / 张
OpenAI 兼容
当前站内已经把 `image2` 渠道接入到 `image2分组`。为了兼容现有图片路由,外部请求时请使用公开模型名 `gpt-image-2`,不要直接传 `image2`。
这条便宜渠道当前只用于 1K 尺寸。如果你传 2K / 4K 或未验证尺寸,不在这份文档的保证范围内。
1. 当前站内接入规则
| 项目 | 当前规则 |
|---|---|
| 文生图地址 | https://ai.cozesf.cc/v1/images/generations |
| 图生图地址 | https://ai.cozesf.cc/v1/images/edits |
| 认证方式 | Authorization: Bearer <你的站内 API Key> |
| 外部可用模型名 | gpt-image-2 |
| 站内实际转发渠道 | image2 |
| 当前推荐尺寸 | 只使用下方列出的 1K 尺寸预设 |
| 默认返回 | 建议显式传 response_format;传 url 读 data[0].url,传 b64_json 读 data[0].b64_json |
| 图生图图片字段 | JSON 传 images[].image_url;本地文件上传使用字段名 image |
| 当前不建议使用 | model=image2、/v1/chat/completions 这两种写法当前不作为站内标准接入方式 |
2. 接入步骤
- 在控制台创建或获取一个已经分配到 `image2分组` 的站内 API Key。
- 文生图使用 `https://ai.cozesf.cc/v1/images/generations`;图生图使用 `https://ai.cozesf.cc/v1/images/edits`。
- 请求体里把模型写成 `gpt-image-2`,不要直接写 `image2`。
- 尺寸只使用 1K 预设。当前这条 Image2 渠道按 0.2 / 张 计费。
- 图生图如果传图片 URL,正式公网入口的 JSON 写法使用 `images` 数组,里面放 `image_url`;如果传本地文件,表单字段名固定用 `image`。
- 建议显式传 `response_format`;传 `url` 读 `data[0].url`,传 `b64_json` 读 `data[0].b64_json`。
3. 文生图请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 固定传 gpt-image-2。 |
| prompt | string | 是 | 图片提示词。 |
| size | string | 否 | 使用本文档列出的 1K 尺寸,例如 1024x1024、720x1280。 |
| response_format | string | 否 | 可选。建议显式传 url 或 b64_json。 |
4. 图生图请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 固定传 gpt-image-2。 |
| prompt | string | 是 | 描述你希望如何改图。 |
| images | array<object> | JSON 时是 | 正式公网入口的 JSON 图生图写法。当前实测可用格式是 [{ "image_url": "https://..." }]。 |
| image | file | Multipart 时是 | 本地文件上传时使用这个字段名。 |
| size | string | 否 | 同样只使用本文档列出的 1K 尺寸。 |
| response_format | string | 否 | 可选。建议显式传 url 或 b64_json。 |
5. 1K 尺寸预设
当前这条 Image2 渠道只按 1K 生图文档范围提供支持。建议从下面这些尺寸里选。
| 比例 | 推荐 size |
|---|---|
| 1:1 | 1024x1024 |
| 16:9 | 1280x720 |
| 9:16 | 720x1280 |
| 3:2 | 1248x832 |
| 2:3 | 832x1248 |
| 4:3 | 1152x864 |
| 3:4 | 864x1152 |
| 5:4 | 1120x896 |
| 4:5 | 896x1120 |
| 21:9 | 1456x624 |
6. 调用示例
文生图 cURL
curl https://ai.cozesf.cc/v1/images/generations \
-H "Authorization: Bearer <你的站内API Key>" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A tiny mint camera icon on a white background",
"size": "1024x1024"
}'
文生图 Python
import requests
url = "https://ai.cozesf.cc/v1/images/generations"
headers = {
"Authorization": "Bearer <你的站内API Key>",
"Content-Type": "application/json",
}
payload = {
"model": "gpt-image-2",
"prompt": "A tiny mint camera icon on a white background",
"size": "1024x1024",
}
resp = requests.post(url, headers=headers, json=payload, timeout=240)
resp.raise_for_status()
print(resp.json())
文生图 JavaScript
const response = await fetch("https://ai.cozesf.cc/v1/images/generations", {
method: "POST",
headers: {
"Authorization": "Bearer <你的站内API Key>",
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "gpt-image-2",
prompt: "A tiny mint camera icon on a white background",
size: "1024x1024"
})
});
const result = await response.json();
console.log(result);
图生图 JSON(传图片 URL)
curl https://ai.cozesf.cc/v1/images/edits \
-H "Authorization: Bearer <你的站内API Key>" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "Turn this image into a clean green flat badge style illustration",
"size": "1024x1024",
"images": [
{
"image_url": "https://ai.cozesf.cc/logo.png"
}
],
"response_format": "url"
}'
图生图 Multipart(上传本地文件)
curl https://ai.cozesf.cc/v1/images/edits \
-H "Authorization: Bearer <你的站内API Key>" \
-F "image=@/path/to/logo.png" \
-F "model=gpt-image-2" \
-F "prompt=Turn this image into a clean green flat badge style illustration" \
-F "size=1024x1024" \
-F "response_format=url"
7. 响应格式
文生图和图生图当前都兼容同一套图片 API 响应结构。若显式传 response_format=url,优先读取 data[0].url;若显式传 b64_json,优先读取 data[0].b64_json。
{
"created": 1786438008,
"data": [
{
"b64_json": "iVBORw0KGgoAAA..."
}
]
}
8. Base64 转图片示例
import base64
import requests
url = "https://ai.cozesf.cc/v1/images/generations"
headers = {
"Authorization": "Bearer <你的站内API Key>",
"Content-Type": "application/json",
}
payload = {
"model": "gpt-image-2",
"prompt": "A tiny mint camera icon on a white background",
"size": "1024x1024",
}
resp = requests.post(url, headers=headers, json=payload, timeout=240)
resp.raise_for_status()
result = resp.json()
b64_data = result["data"][0]["b64_json"]
with open("image2-result.png", "wb") as f:
f.write(base64.b64decode(b64_data))
9. 计费说明
| 档位 | 当前组价格 | 说明 |
|---|---|---|
| 1K | 0.2 / 张 | 当前 Image2 渠道文档范围。本文档示例全部按这一档编写。 |
| 2K | 0.3 / 张 | 不属于这条 1K Image2 便宜渠道的保证范围。 |
| 4K | 0.5 / 张 | 不属于这条 1K Image2 便宜渠道的保证范围。 |
10. 常见错误
| 现象 | 原因 | 处理方式 |
|---|---|---|
| 401 / 403 | 站内 API Key 无效,或者没有分配到正确分组。 | 换成站内有效 Key,并确认它属于 image2分组。 |
| 400 images endpoint requires an image model, got "image2" | 把站内公开模型名写成了 image2。 | 改成 gpt-image-2。 |
| 502 / 503 | 上游渠道暂时不可用,或传了当前未支持的尺寸。 | 优先改用本文档给出的 1K 尺寸;如果仍失败,再检查上游状态。 |
| images[].image_url is required | 正式公网入口的 JSON 图生图没有按当前要求传 images[].image_url。 | 把 JSON 请求改成 "images": [{"image_url": "https://..."}]。 |
| image is required | Multipart 图生图没有带图片,或者表单字段名写错了。 | 文件上传模式使用字段名 image。 |
| /v1/chat/completions 返回不可用 | 当前这条 Image2 接入文档只覆盖图片原生接口。 | 请改用 /v1/images/generations 或 /v1/images/edits。 |