> ## 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>
