> ## Documentation Index
> Fetch the complete documentation index at: https://docs.toapis.com/llms.txt
> Use this file to discover all available pages before exploring further.

# GPT-Image-2 官方渠道 圖像編輯

> 基於參考圖進行圖像編輯 / 圖生圖 / 局部重繪（mask inpainting），使用 gpt-image-2 模型

* 與圖像生成使用同一端點 `/v1/images/generations`
* 請求中包含 `image_urls` 即自動切換爲編輯模式
* 支持多參考圖融合（最多 16 張）、局部重繪（mask）
* 參考圖須爲公網可訪問的 PNG / JPG，單張 ≤ 50MB

<Note>
  編輯模式的調用方式與生成完全兼容：只需在生成請求基礎上加入 `image_urls`（和可選的 `mask_url`）即可，無需切換端點。
</Note>

## Authorizations

<ParamField header="Authorization" type="string" required>
  所有接口均需要使用 Bearer Token 進行認證

  獲取 API Key：訪問 [API Key 管理頁面](https://toapis.com/console/token) 獲取您的 API Key

  ```
  Authorization: Bearer YOUR_API_KEY
  ```
</ParamField>

## Body

<ParamField body="model" type="string" default="gpt-image-2" required>
  固定填寫 `gpt-image-2`
</ParamField>

<ParamField body="prompt" type="string" required>
  描述期望的編輯效果

  示例：`"把背景換成星空，保持主體人物不變"`
</ParamField>

<ParamField body="image_urls" type="string[]" required>
  參考圖 URL 數組（觸發編輯模式的關鍵字段）

  * 最多 **16 張**
  * 必須是公網可訪問的穩定 URL（PNG / JPG，單張 ≤ 50MB）
  * 可使用 [上傳圖片接口](../../uploads/images) 獲取 URL
</ParamField>

<ParamField body="mask_url" type="string">
  遮罩圖 URL，用於局部重繪（inpainting）

  * 須爲 PNG 格式，且包含 Alpha 通道
  * 透明區域（alpha = 0）爲待重繪區域，不透明區域保持原樣
  * 尺寸須與第一張參考圖一致
</ParamField>

<ParamField body="size" type="string" default="1:1">
  輸出圖像寬高比

  支持 13 種比例，也可傳 `auto`：

  `1:1` · `3:2` · `2:3` · `4:3` · `3:4` · `5:4` · `4:5` · `16:9` · `9:16` · `2:1` · `1:2` · `21:9` · `9:21`
</ParamField>

<ParamField body="resolution" type="string" default="1k">
  分辨率檔位：`1k` / `2k` / `4k`

  1K / 2K 支持全部 13 個比例；4K 僅支持 6 個比例（`16:9` / `9:16` / `2:1` / `1:2` / `21:9` / `9:21`）
</ParamField>

<ParamField body="quality" type="string" default="high">
  圖片質量：`low` / `medium` / `high`
</ParamField>

<ParamField body="n" type="integer" default={1}>
  生成數量，範圍 `1 ~ 10`
</ParamField>

<ParamField body="output_format" type="string" default="png">
  輸出格式：`png` / `jpeg`

  <Note>Azure OpenAI 不支持 `webp` 格式。</Note>
</ParamField>

<ParamField body="output_compression" type="integer" default={100}>
  JPEG 壓縮強度，範圍 `0–100`（僅對 `output_format: "jpeg"` 有效）
</ParamField>

## Response

<ResponseField name="id" type="string">
  任務唯一標識符，用於查詢任務狀態
</ResponseField>

<ResponseField name="object" type="string">
  固定爲 `generation.task`
</ResponseField>

<ResponseField name="status" type="string">
  任務狀態：`queued` / `in_progress` / `completed` / `failed`
</ResponseField>

<ResponseField name="progress" type="integer">
  任務進度百分比（0-100）
</ResponseField>

<ResponseField name="created_at" type="integer">
  任務創建時間戳（Unix 時間戳）
</ResponseField>

<RequestExample>
  ```bash 圖生圖（多參考圖融合） theme={null}
  curl --request POST \
    --url https://toapis.com/v1/images/generations \
    --header 'Authorization: Bearer <token>' \
    --header 'Content-Type: application/json' \
    --data '{
      "model": "gpt-image-2",
      "prompt": "將兩張參考圖融合成賽博朋克風格插畫海報",
      "image_urls": [
        "https://example.com/input-a.png",
        "https://example.com/input-b.png"
      ],
      "size": "1:1",
      "quality": "high",
      "n": 1
    }'
  ```

  ```bash 局部重繪（mask inpainting） theme={null}
  curl --request POST \
    --url https://toapis.com/v1/images/generations \
    --header 'Authorization: Bearer <token>' \
    --header 'Content-Type: application/json' \
    --data '{
      "model": "gpt-image-2",
      "prompt": "把背景換成沙漠日落，保持人物不變",
      "image_urls": ["https://example.com/photo.png"],
      "mask_url": "https://example.com/mask.png",
      "size": "1:1",
      "quality": "medium"
    }'
  ```

  ```bash 風格遷移（高清輸出） theme={null}
  curl --request POST \
    --url https://toapis.com/v1/images/generations \
    --header 'Authorization: Bearer <token>' \
    --header 'Content-Type: application/json' \
    --data '{
      "model": "gpt-image-2",
      "prompt": "將照片轉換爲水彩畫風格，保留場景構圖",
      "image_urls": ["https://example.com/landscape.jpg"],
      "size": "16:9",
      "resolution": "2k",
      "quality": "high",
      "output_format": "jpeg",
      "output_compression": 85
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://toapis.com/v1/images/generations', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer your-ToAPIs-key',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      model: 'gpt-image-2',
      prompt: '將兩張參考圖融合成賽博朋克風格插畫海報',
      image_urls: [
        'https://example.com/input-a.png',
        'https://example.com/input-b.png',
      ],
      size: '1:1',
      quality: 'high',
      n: 1,
    }),
  });

  const task = await response.json();
  console.log(task.id, task.status);
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "tsk_img_01KPTXXXXXXXXXXXXXXX",
    "object": "generation.task",
    "model": "gpt-image-2",
    "status": "queued",
    "progress": 0,
    "created_at": 1703884800,
    "metadata": {}
  }
  ```
</ResponseExample>
