画像から画像への変換(画像編集)
BetterToken の OpenAI 互換 API を通じて、GPT Image 2 で参照画像から画像を生成または編集します。
POST /v1/images/edits
画像から画像への変換 Endpoint では multipart/form-data のリクエスト本文を使用します。1 枚以上の参照画像をアップロードし、プロンプトとパラメーターフィールドをフォームフィールドとして送信します。
https://bettertoken.ai/v1 を Base URL として使用します。BetterToken API Key は Authorization: Bearer YOUR_API_KEY で渡します。推奨値
すべてのリクエストで、次のフィールドを明示的に送信してください。image を使用します。複数の参照画像には image[] フィールドを繰り返し使用します。
1 枚の参照画像
複数の参照画像
マスクで選択範囲を編集する
選択した範囲の編集を指示するには、任意のmask ファイルフィールドを追加します。現在の BetterToken エンドポイントが読み込む mask ファイルは 1 つです。mask[] フィールドを複数送信しないでください。
- 元画像とマスクは、ファイル形式とピクセル寸法を同じにする必要があります。各ファイルは 50 MB 未満にしてください。最大限の互換性を得るには、両方のファイルに Alpha チャンネル付き PNG を使用することを推奨します。
- マスクには Alpha チャンネルが必要です。完全に透明なピクセル(
Alpha 0)は、モデルが再描画できる範囲を示します。完全に不透明なピクセル(Alpha 255)は、その範囲を保持するようモデルを誘導します。 - 通常の RGB 白黒画像は、実際に Alpha チャンネルを含んでいない限り、有効なマスクではありません。
- 複数の
image[]フィールドを送信した場合、マスクは最初の入力画像に適用されます。 - マスクはモデルへの指示です。透明な範囲の外側にあるピクセルが完全に同一のままになることを保証するものではありません。
Python でマスク編集する
リクエスト後に Python が両方のファイルを閉じるよう、1 つのwith 文で開きます。
アップロード前にファイルを確認する
この最小限の Pillow チェックで、必須の形式、寸法、Alpha チャンネルを確認できます。一般的な局所マスクについては、Alpha チャンネルに完全に透明な領域と完全に不透明な領域の両方がない場合に警告も表示します。Python の例
推奨サイズ
size は想定するアスペクト比とサイズ区分を表します。実際に返されるピクセルはサーバーでマッピングまたは調整される場合があります。出力を要求値に無理に切り抜くのではなく、デコードした画像の寸法を使用してください。
画像を保存する
成功時のレスポンスは、次の OpenAI 互換画像レスポンス形式に従います。data[0].b64_json を読み取り、base64 の画像内容として保存します。常に output_format: "png" を設定すると、デコードした画像を .png として保存できます。
レスポンスの流れ
この Endpoint は同期式です。POST /images/edits を送信した後、サーバーが応答するまで現在の HTTP リクエストを開いたままにしてください。生成に成功すると、画像内容が data[0].b64_json に返されます。
この Endpoint は task_id を返さず、個別のステータス照会や結果ダウンロード用の Endpoint もありません。
関連ドキュメント
承認
Bearer authentication header of the form Bearer <token>, where <token> is your auth token.
ボディ
Use the GPT Image 2 model ID.
gpt-image-2 "gpt-image-2"
Instructions for editing the image or generating from reference images.
"Use this reference image to create a more polished square product hero image while preserving the main style."
The reference image field for a single input image.
Repeat this field for each input when sending multiple reference images.
An optional single mask file. The mask and original image must have the same format and dimensions, each file must be smaller than 50 MB, and the mask must contain an Alpha channel. For maximum compatibility, use PNG with an Alpha channel for both files. With multiple image[] inputs, the mask applies to the first image.
Use 1. Send separate requests when you need multiple output images.
"1"
The requested image size or aspect-ratio tier. The server may map or adjust the final pixel dimensions, so inspect the decoded image for its actual size.
auto, 1024x1024, 1536x1024, 1024x1536, 1536x1152, 1152x1536, 2048x2048, 2048x1152, 3840x2160, 2160x3840 "1024x1024"
Use b64_json.
b64_json "b64_json"
Use png. Do not rely on jpeg or webp being returned directly in the selected format.
png "png"

