> ## 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 图片编辑](/cn/api-reference/endpoint/gpt-image-edit)
