OpenAI 创建图像变体接口

2026-11-11 10:12 更新

创建图像变体(历史接口)

post /images/variations

此接口已停用。可改用图像编辑接口,配合 GPT Image 模型和提示词创建图像变体。以下请求与响应结构描述的是历史接口规范。

请求体参数

内容类型为 multipart/form-data。

  • image: string

用于生成变体的基础图像,必须为有效的正方形 PNG 文件,大小小于 4 MB。

  • model: optional string or ImageModel or null (default: "dall-e-2")

已停用图像变体接口使用的历史模型。此接口已不再接受请求。

  • string

  • ImageModel = "gpt-image-1.5" or "gpt-image-2" or "gpt-image-2-2026-04-21" or 11 more

  • "gpt-image-1.5"

  • "gpt-image-2"

  • "gpt-image-2-2026-04-21"

  • "gpt-image-2.5-sunburst"

  • "gpt-image-2.5-sunburst-2026-09-08"

  • "gpt-image-2.5-flare"

  • "gpt-image-2.5-flare-2026-09-08"

  • "gpt-image-2.6-sunburst"

  • "gpt-image-2.6-flare"

  • "gpt-image-1"

  • "gpt-image-1-mini"

  • "chatgpt-image-latest"

  • "dall-e-2"

  • "dall-e-3"

  • n: optional integer or null (default: 1)

生成图像的数量,范围为 1–10。

minimum: 1, maximum: 10

  • response_format: optional "url" or "b64_json" or null (default: "url")

生成图像的返回格式,必须为 url 或 b64_json。URL 在图像生成后仅有效 60 分钟。

  • "url"

  • "b64_json"

  • size: optional "256x256" or "512x512" or "1024x1024" or null (default: "1024x1024")

生成图像的尺寸,必须为 256x256、512x512 或 1024x1024。

  • "256x256"

  • "512x512"

  • "1024x1024"

  • user: optional string

代表终端用户的唯一标识,可帮助 OpenAI 监测和识别滥用。参阅安全标识说明。

返回值

  • ImagesResponse object { created, background, data, 6 more }

图像生成接口的响应。

  • created: integer

图像创建时间,采用 Unix 时间戳,单位为秒。

format: unixtime

  • background: optional "transparent" or "opaque"

生成图像使用的背景参数,值为 transparent 或 opaque。

  • "transparent"

  • "opaque"

  • data: optional array of Image

生成的图像列表。

  • b64_json: optional string

包含 Base64 编码图像的 JSON 字段。GPT Image 模型默认返回;支持 response_format 的模型在设为 b64_json 时返回。

  • revised_prompt: optional string

支持提示词改写的模型用于生成图像的改写后提示词。GPT Image 模型不返回此字段。

  • url: optional string

支持该参数的模型在 response_format 设为 url 时返回的生成图像 URL。GPT Image 模型不支持此返回方式。

format: uri

  • output_format: optional "png" or "webp" or "jpeg" or 2 more

生成或编辑后图像的输出格式。

  • "png"

  • "webp"

  • "jpeg"

  • "gif"

  • "svg"

  • quality: optional "low" or "medium" or "high" or 2 more

生成图像的质量,可选 low、medium、high、xhigh 或 max。

  • "low"

  • "medium"

  • "high"

  • "xhigh"

  • "max"

  • safety_identifier: optional string or null

用于安全监测和滥用检测的实际安全标识。优先使用请求中非空的 safety_identifier;如果省略、为 null 或为空,则使用已弃用的 user 值。

  • size: optional string or "1024x1024" or "1024x1536" or "1536x1024"

图像尺寸,采用 WIDTHxHEIGHT 字符串形式,例如 1536x864。

  • string

  • "1024x1024" or "1024x1536" or "1536x1024"

图像尺寸,采用 WIDTHxHEIGHT 字符串形式,例如 1536x864。

  • "1024x1024"

  • "1024x1536"

  • "1536x1024"

  • svg_usage: optional object { input_tokens, model, output_tokens, 2 more }

SVG 编写的词元用量,与图像渲染用量分别记录。

  • input_tokens: integer

  • model: string

  • output_tokens: integer

  • reasoning_tokens: integer

  • total_tokens: integer

  • usage: optional object { input_tokens, input_tokens_details, output_tokens, 2 more }

GPT Image 模型生成图像的词元用量。

  • input_tokens: integer

输入提示词中的图像与文本词元数量。

  • input_tokens_details: object { image_tokens, text_tokens }

图像生成的输入词元明细。

  • image_tokens: integer

输入提示词中的图像词元数量。

  • text_tokens: integer

输入提示词中的文本词元数量。

  • output_tokens: integer

模型生成的输出词元数量。

  • total_tokens: integer

图像生成使用的图像和文本词元总数。

  • output_tokens_details: optional object { image_tokens, text_tokens }

图像生成的输出词元明细。

  • image_tokens: integer

模型生成的图像输出词元数量。

  • text_tokens: integer

模型生成的文本输出词元数量。

示例

## 设置 OPENAI_API_KEY,并根据示例替换文件路径或会话参数。
curl https://api.openai.com/v1/images/variations \
    -H 'Content-Type: multipart/form-data' \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -F 'image=@/path/to/image' \
    -F model=dall-e-2 \
    -F n=1 \
    -F response_format=url \
    -F size=1024x1024 \
    -F user=user-1234

200 响应示例

{
  "created": 0,
  "background": "transparent",
  "data": [
    {
      "b64_json": "b64_json",
      "revised_prompt": "revised_prompt",
      "url": "https://example.com"
    }
  ],
  "output_format": "png",
  "quality": "low",
  "safety_identifier": "safety_identifier",
  "size": "1024x1024",
  "svg_usage": {
    "input_tokens": 0,
    "model": "model",
    "output_tokens": 0,
    "reasoning_tokens": 0,
    "total_tokens": 0
  },
  "usage": {
    "input_tokens": 0,
    "input_tokens_details": {
      "image_tokens": 0,
      "text_tokens": 0
    },
    "output_tokens": 0,
    "total_tokens": 0,
    "output_tokens_details": {
      "image_tokens": 0,
      "text_tokens": 0
    }
  }
}

示例

## 设置 OPENAI_API_KEY,并根据示例替换文件路径或会话参数。
curl https://api.openai.com/v1/images/variations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F image="@otter.png" \
  -F n=2 \
  -F size="1024x1024"

响应示例

{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    },
    {
      "url": "https://..."
    }
  ]
}
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号