跳到主要内容

GPT-Image-2

本页示例中的 {BASE_URL} 表示 https://api.uniall.ai。

gpt-image-2 可通过 OpenAI 兼容图像接口进行文生图和图像编辑。该模型使用 size 同时控制尺寸和画幅比例,不支持 aspect_ratio。

Endpoint​

POST /v1/images/generations
POST /v1/images/edits

文生图使用 /v1/images/generations。包含参考图的编辑请求使用 /v1/images/edits。

Authentication​

Authorization: Bearer sk-***
Content-Type: application/json

请求参数​

参数类型必填说明
modelstring是固定使用 gpt-image-2。
promptstring是图片生成或编辑提示词。
imagestring/object条件必填编辑图片时的参考图。
imagesarray条件必填支持多图时使用的参考图数组。
sizestring是输出尺寸,例如 1024x1024。
qualitystring否low、medium 或 high。默认 medium。
output_formatstring否png、jpeg 或 webp。
backgroundstring否auto(模型根据提示词决定背景)、opaque(不透明背景)或 transparent(透明背景)。省略或传 null 时默认 auto。
ninteger否输出图片数量,默认 1,范围为 1 到 8。
response_formatstring否生成资产建议使用 url。

背景设置​

背景参数待上线。background 放在请求体顶层。

  • auto:模型根据提示词决定背景;省略或传 null 时使用此默认值。
  • opaque:指定不透明背景。
  • transparent:指定透明背景;output_format 必须为 png 或 webp,未传时默认 png。

非法 background 值,或 transparent 与 jpeg 的组合,会在请求校验阶段返回 HTTP 400,不会开始生成。

适用于本页的同步生成和编辑接口。 既有 GPT Image 2 异步图片任务和图片聊天兼容请求也使用同一顶层参数。图片结果仍返回 URL。

文生图示例​

curl -X POST "{BASE_URL}/v1/images/generations" \
-H "Authorization: Bearer sk-***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "An orange cat sticker, full subject visible, transparent background",
"size": "1024x768",
"quality": "low",
"background": "transparent",
"output_format": "webp",
"n": 1,
"response_format": "url"
}'

图像编辑示例​

curl -X POST "{BASE_URL}/v1/images/edits" \
-H "Authorization: Bearer sk-***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "Keep the product shape and remove the background to make it transparent",
"image": "https://example.com/input.png",
"size": "1024x1024",
"quality": "medium",
"background": "transparent",
"output_format": "png",
"n": 1,
"response_format": "url"
}'

响应示例​

{
"created": 1778688000,
"data": [
{
"url": "https://example.com/generated.png"
}
]
}

常见错误​

  • 缺少 size。
  • 传入 aspect_ratio;该模型应使用 size。
  • quality 不在 low、medium、high 范围内。
  • n 不在 1 到 8 范围内。
  • 使用了当前账号不支持的尺寸。

相关页面​