建立圖片
使用 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等控制項的文字轉圖片請求,請使用background等 GPT 圖片模型 - 需要透明背景或
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..."
}
]
}