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

# GPT Image

> GPT Image 圖片生成欄位、模型限制與請求示例。

使用 OpenAI Images 請求格式，通過 GPT Image 根據文本生成圖片。

`POST https://api.mixroute.ai/v1/images/generations`

## 支援模型

| 模型 ID           | 能力與限制                 |
| --------------- | --------------------- |
| `gpt-image-2`   | 生成與編輯，支援靈活尺寸，輸入固定高保真。 |
| `gpt-image-1.5` | 生成與編輯，使用標準圖片尺寸。       |
| `gpt-image-1`   | 生成與編輯，使用標準圖片尺寸。       |

## 請求參數

| 欄位                   | 型別      | 必填 | 說明                                                                                |
| -------------------- | ------- | -- | --------------------------------------------------------------------------------- |
| `model`              | string  | 是  | 使用上表完整模型 ID，顯式指定呼叫模型。                                                             |
| `prompt`             | string  | 是  | 生成或編輯指令，最多 32000 個字元。                                                             |
| `n`                  | integer | 否  | 輸出圖片數量，範圍 1-10，預設 1。                                                              |
| `size`               | string  | 否  | 輸出尺寸，或使用預設值 `auto`；具體規則見下方版本說明。                                                   |
| `quality`            | string  | 否  | `low`、`medium`、`high` 或預設值 `auto`。                                                |
| `background`         | string  | 否  | `auto`（預設）、`opaque` 或 `transparent`。透明背景須使用 PNG 或 WebP；GPT Image 2 的透明背景能力處於預覽階段。 |
| `moderation`         | string  | 否  | `auto`（預設）或 `low`，用於控制模型內容過濾等級。                                                   |
| `output_format`      | string  | 否  | 輸出圖片編碼格式：`png`（預設）、`jpeg` 或 `webp`。                                               |
| `output_compression` | integer | 否  | 壓縮設定，範圍 0-100，預設 100，僅用於 JPEG 或 WebP 輸出。                                          |
| `stream`             | boolean | 否  | `true` 時以 SSE 返回結果，預設 `false`。                                                    |
| `partial_images`     | integer | 否  | 流式響應中的中間圖片數量，範圍 0-3；為 0 時只返回最終圖片。生成提前完成時，中間圖片可能少於指定數量。                            |
| `user`               | string  | 否  | 用於濫用監測的穩定終端使用者標識，避免直接提供個人資訊。                                                      |

## 尺寸規則

| 模型                | 允許尺寸                                                                        |
| ----------------- | --------------------------------------------------------------------------- |
| GPT Image 1 / 1.5 | `auto`, `1024x1024`, `1536x1024`, `1024x1536`                               |
| GPT Image 2       | `auto` 或 `寬x高`。寬高均為 16 的倍數，單邊不超過 3840 px，長短邊比例不超過 3:1，總畫素數為 655360-8294400。 |

GPT Image 2 超過 3686400 總畫素的解析度屬於實驗性範圍。此處 GPT 模型返回 Base64 圖片資料，不傳 DALL-E 專用的 `response_format` 或 `style`。

## 呼叫示例

將 MixRoute Key 設定到環境變數 `MIXROUTE_API_KEY`，通過 `Authorization: Bearer ...` 認證。編輯圖片前，將素材佔位值替換為可訪問的圖片或本地檔案。

```bash theme={null}
curl --request POST "https://api.mixroute.ai/v1/images/generations" \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "gpt-image-2",
  "prompt": "A clean product photograph of a red ceramic mug on a white background.",
  "size": "1024x1024",
  "quality": "low",
  "n": 1,
  "output_format": "png"
}'
```

### Python

```python theme={null}
import json
import os
import requests

payload = json.loads(r'''
{
  "model": "gpt-image-2",
  "prompt": "A clean product photograph of a red ceramic mug on a white background.",
  "size": "1024x1024",
  "quality": "low",
  "n": 1,
  "output_format": "png"
}
''')
response = requests.post(
    "https://api.mixroute.ai/v1/images/generations",
    headers={"Authorization": "Bearer " + os.environ["MIXROUTE_API_KEY"]},
    json=payload,
    timeout=180,
)
response.raise_for_status()
result = response.json()
import base64
from pathlib import Path

for index, item in enumerate(result["data"]):
    Path(f"image_{index}.png").write_bytes(base64.b64decode(item["b64_json"]))
```

## 響應

從 `data[].b64_json` 讀取圖片，並按請求的 `output_format` 解碼儲存。流式響應返回中間圖片和完成事件，最終檔案應使用完成事件中的資料。

[GPT Image 圖片編輯](/zh-hant/api-reference/endpoint/gpt-image-edit)
