创建图像
使用 CometAPI POST /v1/images/generations 通过兼容 OpenAI 的图像模型和模型专属控件创建图像。
curl https://api.cometapi.com/v1/images/generations \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A paper boat floating on calm water at sunrise.",
"quality": "low",
"size": "1024x1024",
"output_format": "jpeg"
}'{
"created": 1781075000,
"background": "opaque",
"output_format": "jpeg",
"quality": "low",
"size": "1024x1024",
"usage": {
"input_tokens": 13,
"input_tokens_details": {
"image_tokens": 0,
"text_tokens": 13
},
"output_tokens": 196,
"output_tokens_details": {
"image_tokens": 196,
"text_tokens": 0
},
"total_tokens": 209
},
"data": [
{
"b64_json": "/9j/4AAQSkZJRgABAQAAAQABAAD..."
}
]
}官方参考
- 在依赖模型专属控件之前,请阅读 OpenAI 图像生成指南 ,其中包括
background、output_compression、流式输出或未来的 GPT 图像选项等。 - 请使用 OpenAI 创建图像参考 查看当前参数列表。
先选择模型
- 对于具有
gpt-image-2、output_format或quality等控件的文生图请求,请使用 GPT 图像模型,例如background - 当需要透明背景或
gpt-image-2.5-sunburst和gpt-image-2.5-flare质量级别时,请使用xhigh或max - 其他图像模型系列也使用相同的请求格式,例如 Qwen-Image、Doubao Seedream 或 Grok Imagine;请先使用
model和prompt - 请从以下页面选择当前图像模型 ID: 模型页面
安全的首个请求
- 从
gpt-image-2开始 - 将
size保持为1024x1024 - GPT 图像模型会在
b64_json中返回经过 base64 编码的图像数据;请对其解码以保存图像文件 - 仅当需要特定的编码图像类型(如
output_format)时,才添加jpeg - 在添加批量生成或风格调优之前,请先使用一个 Prompt 和一张输出图像
运行异步图像任务
当图像作业耗时较长且客户端希望采用提交并轮询流程,而非保持一个 HTTP 连接处于打开状态时,请使用async: true。创建请求会返回 data.task_id。
轮询 检索图像生成任务 ,直到 data.status 为 success 或 failure。
async 字段是此路由的 CometAPI 扩展,并非 OpenAI 参数。OpenAI 为 GPT 图像模型记录了 stream 和 partial_images。CometAPI 异步任务模式返回 JSON 任务元数据并使用轮询。
请对以下已记录的模型 ID 使用异步任务模式:gpt-image-2 和 doubao-seedream-4-0-250828。对于其他图像模型,请使用同步生成或流式输出,除非该模型已记录支持异步任务。
当请求同时包含 async: true 和 stream: true 时,异步任务模式优先。创建请求会返回 JSON 任务元数据,而不是 SSE 流。
模型专属请求行为
response_format仅适用于 DALL·E 模型;GPT 图像模型返回 base64 数据并会忽略它- GPT 图像模型使用 GPT-only 控件,例如
output_format、quality、background和output_compression quality在 GPT 图像模型上接受low、medium、high和auto;gpt-image-2.5-sunburst和gpt-image-2.5-flare还接受xhigh和maxoutput_compression当output_format为webp或jpeg时适用;它对png无效partial_images仅当stream为true时适用- 请参阅 OpenAI 图像生成指南,了解最新的模型专属选项
- 其他图像模型系列可能会在
url中返回图像,而非b64_json;请读取每个结果项所包含的字段
生成透明背景
将background 设置为 transparent,即可生成没有背景填充的孤立主体。gpt-image-2.5-sunburst 和 gpt-image-2.5-flare 支持此功能。
透明效果需要使用带 alpha 通道的输出格式。将 output_format 设置为 png 或 webp。JPEG 不含 alpha 通道,因此使用 output_format: "jpeg" 发出的透明请求会被拒绝。
响应会在 b64_json 中返回带 alpha 通道的 base64 图像数据。对其进行解码以保存文件:
import base64
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.cometapi.com/v1",
api_key=os.environ["COMETAPI_KEY"],
)
result = client.images.generate(
model="gpt-image-2.5-sunburst",
prompt="A single red apple, centered, isolated subject.",
quality="low",
size="1024x1024",
background="transparent",
output_format="png",
)
with open("apple.png", "wb") as f:
f.write(base64.b64decode(result.data[0].b64_json))
background 设置为 opaque 以强制使用纯色背景,或设置为 auto 以让模型决定。响应会在顶层 background 字段中回显应用的值。
请求体
The image generation model to use. Choose a current model from the Models page.
Quality setting for models that support it. GPT image models accept low, medium, high, and auto. gpt-image-2.5-sunburst and gpt-image-2.5-flare also accept xhigh and max. dall-e-3 accepts standard and hd. See the OpenAI image generation guide for the latest model-specific values.
"low"
Background mode for the generated image. Set transparent to generate an isolated subject with no background fill; this requires output_format set to png or webp, and returns an error with jpeg. Set opaque for a solid background, or auto to let the model decide. Supported on gpt-image-2.5-sunburst and gpt-image-2.5-flare.
transparent, opaque, auto Set this to true to receive server-sent image generation events instead of waiting for the completed JSON response. Streaming responses use text/event-stream and can include final events such as image_generation.completed. When stream and async are both true, async task mode takes precedence and the create request returns JSON instead of a streaming image response.
CometAPI asynchronous task mode. Set this to true to return immediately with data.task_id, then poll GET /v1/images/generations/{task_id} for the final image data. Documented model IDs for this mode: gpt-image-2 and doubao-seedream-4-0-250828. This is a CometAPI extension, not an OpenAI parameter. When async and stream are both true, async takes precedence and returns JSON task metadata instead of an SSE stream.
响应
Image generation result. Synchronous requests return completed image data. Async requests return a task response with data.task_id.
- Completed image response
- Async task response
curl https://api.cometapi.com/v1/images/generations \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A paper boat floating on calm water at sunrise.",
"quality": "low",
"size": "1024x1024",
"output_format": "jpeg"
}'{
"created": 1781075000,
"background": "opaque",
"output_format": "jpeg",
"quality": "low",
"size": "1024x1024",
"usage": {
"input_tokens": 13,
"input_tokens_details": {
"image_tokens": 0,
"text_tokens": 13
},
"output_tokens": 196,
"output_tokens_details": {
"image_tokens": 196,
"text_tokens": 0
},
"total_tokens": 209
},
"data": [
{
"b64_json": "/9j/4AAQSkZJRgABAQAAAQABAAD..."
}
]
}