> ## 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/edits`

## 支持模型

| 模型 ID           | 能力与限制                 |
| --------------- | --------------------- |
| `gpt-image-2`   | 生成与编辑，支持灵活尺寸，输入固定高保真。 |
| `gpt-image-1.5` | 生成与编辑，使用标准图片尺寸。       |
| `gpt-image-1`   | 生成与编辑，使用标准图片尺寸。       |

## 请求参数

| 字段                   | 类型              | 必填 | 说明                                                                                |
| -------------------- | --------------- | -- | --------------------------------------------------------------------------------- |
| `model`              | string          | 是  | 使用上表完整模型 ID，显式指定调用模型。                                                             |
| `prompt`             | string          | 是  | 生成或编辑指令，最多 32000 个字符。                                                             |
| `image`              | file \| file\[] | 是  | PNG、JPEG 或 WebP 输入，单张小于 50 MB，最多 16 张。通过 multipart 表单上传，多图可重复使用 `image[]`。        |
| `mask`               | file            | 否  | PNG 蒙版，小于 4 MB，像素尺寸须与第一张源图一致。完全透明区域表示编辑区域；多图请求只对第一张图片应用蒙版。                        |
| `input_fidelity`     | string          | 否  | GPT Image 1 和 1.5 可使用 `high` 或 `low`；GPT Image 2 固定以高保真处理输入，不传此字段。                |
| `n`                  | integer         | 否  | 输出图片数量，范围 1-10，默认 1。                                                              |
| `size`               | string          | 否  | 输出尺寸，或使用默认值 `auto`；具体规则见下方版本说明。                                                   |
| `quality`            | string          | 否  | `low`、`medium`、`high` 或默认值 `auto`。                                                |
| `background`         | string          | 否  | `auto`（默认）、`opaque` 或 `transparent`。透明背景须使用 PNG 或 WebP；GPT Image 2 的透明背景能力处于预览阶段。 |
| `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`。

<Info>
  使用 multipart/form-data，不使用 JSON 图片 URL 对象；由 cURL 或 SDK 设置 multipart boundary。`input_fidelity=auto/medium` 和顶层 `images` JSON 数组不属于此 OpenAI 请求格式。
</Info>

## 调用示例

将 MixRoute Key 设置到环境变量 `MIXROUTE_API_KEY`，通过 `Authorization: Bearer ...` 认证。编辑图片前，将素材占位值替换为可访问的图片或本地文件。

```bash theme={null}
curl --request POST "https://api.mixroute.ai/v1/images/edits" \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --form "model=gpt-image-2" \
  --form "image[]=@source.png" \
  --form "prompt=Make the mug blue while preserving its shape, lighting, and background." \
  --form "size=1024x1024" \
  --form "quality=low" \
  --form "output_format=png"
```

### Python

```python theme={null}
import os
import base64
from pathlib import Path
from openai import OpenAI

client = OpenAI(api_key=os.environ["MIXROUTE_API_KEY"], base_url="https://api.mixroute.ai/v1")
with open("source.png", "rb") as source:
    result = client.images.edit(
        model="gpt-image-2",
        image=source,
        prompt="Make the mug blue while preserving its shape, lighting, and background.",
        size="1024x1024",
        quality="low",
        output_format="png",
    )
Path("edited.png").write_bytes(base64.b64decode(result.data[0].b64_json))
```

局部编辑时增加 `mask` 文件；多图参考时增加图片文件，蒙版仍只作用于第一张图。

## 响应

从 `data[].b64_json` 读取图片，并按请求的 `output_format` 解码保存。流式响应返回中间图片和完成事件，最终文件应使用完成事件中的数据。

[GPT Image 图片生成](/cn/api-reference/endpoint/gpt-image)
