画像を作成
CometAPI POST /v1/images/generations を使用して、OpenAI 互換の画像モデルとモデル固有のコントロールで画像を作成します。
{
"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などの GPT 画像モデルを使用すると、output_format、quality、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 画像モデルは、base64 エンコードされた画像データを
b64_jsonで返します。画像ファイルを保存するにはデコードしてください output_formatは、jpegなど特定のエンコード済み画像タイプが必要な場合にのみ追加します- バッチ生成やスタイル調整を追加する前に、1 つのプロンプトと 1 枚の出力画像を使用してください
非同期画像タスクを実行
クライアントで 1 つの 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 の両方が含まれる場合、非同期タスクモードが優先されます。作成リクエストは SSE ストリームではなく JSON タスクメタデータを返します。
モデル固有のリクエスト動作
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とmaxも受け付けますoutput_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 でサポートされています。
透明化にはアルファチャネルを備えた出力形式が必要です。output_format を png または webp に設定します。JPEG にはアルファチャネルがないため、output_format: "jpeg" を指定した透明化リクエストは拒否されます。
レスポンスでは、アルファチャネルを含む base64 画像データが b64_json に返されます。ファイルとして保存するには、これをデコードします。
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
{
"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..."
}
]
}