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

# OpenAI 图片编辑接口

> 编辑已有图片，兼容 OpenAI Images 协议，支持 Inpainting 局部重绘与 Outpainting 画面扩展

此接口兼容 OpenAI Images 协议，基于输入的源图片与编辑提示词生成新图片，支持局部重绘、画面扩展与多图融合等编辑场景。

## 功能特性

* 支持通过 URL 地址或文件 ID 引用待编辑的源图片
* 支持 Inpainting 局部重绘，通过 `mask` 遮罩指定编辑区域
* 支持 Outpainting 画面扩展，通过 `background` 提供背景图像
* 支持 `input_fidelity` 控制输入图片保真度（`high` / `low`）
* 支持 `png`、`jpeg`、`webp` 输出格式与 `output_compression` 压缩率控制
* 支持 `url` 与 `b64_json` 两种响应格式
* 支持流式（SSE）响应，通过 `partial_images` 推送中间结果

## 认证方式

在请求头中携带 `Authorization` 字段，格式为 `Bearer YOUR_API_KEY`。

## 支持的图片模型

模型广场中可用的图片模型，请参考 [模型列表](https://portal.haitoken.ai/zh/models)。

## 快速示例

<CodeGroup>
  ```python Python theme={null}
  import requests

  url = "https://api.haitoken.ai/v1/images/edits"
  headers = {
      "Authorization": "Bearer YOUR_API_KEY",
      "Content-Type": "application/json"
  }
  data = {
      "model": "gpt-image-2",
      "images": [
          {"image_url": "https://example.com/images/cat.png"}
      ],
      "prompt": "把背景换成日落时分的海边，保持猫咪不变",
      "size": "1024x1024",
      "quality": "high",
      "n": 1
  }

  response = requests.post(url, json=data, headers=headers)
  print(response.json())
  ```

  ```javascript Node.js theme={null}
  const response = await fetch('https://api.haitoken.ai/v1/images/edits', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'gpt-image-2',
      images: [
        { image_url: 'https://example.com/images/cat.png' }
      ],
      prompt: '把背景换成日落时分的海边，保持猫咪不变',
      size: '1024x1024',
      quality: 'high',
      n: 1
    })
  });

  const data = await response.json();
  console.log(data);
  ```

  ```curl cURL theme={null}
  curl -X POST 'https://api.haitoken.ai/v1/images/edits' \
    -H 'Authorization: Bearer YOUR_API_KEY' \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gpt-image-2",
      "images": [
        {"image_url": "https://example.com/images/cat.png"}
      ],
      "prompt": "把背景换成日落时分的海边，保持猫咪不变",
      "size": "1024x1024",
      "quality": "high",
      "n": 1
    }'
  ```
</CodeGroup>

编辑成功后返回图片数据：

```json theme={null}
{
  "created": 1787875200,
  "data": [
    {
      "url": "https://example.com/images/edited-1.png"
    }
  ],
  "size": "1024x1024",
  "quality": "high",
  "usage": {
    "input_tokens": 3280,
    "output_tokens": 4160,
    "total_tokens": 7440,
    "input_tokens_details": {
      "image_tokens": 3268,
      "text_tokens": 12
    },
    "output_tokens_details": {
      "image_tokens": 4160,
      "text_tokens": 0
    }
  }
}
```

## 下一步

* 查看 [OpenAI 图片生成接口](/docs/zh/api-reference/images/openai/generation) 从提示词生成图片
* 查看 [Seedream 图片生成接口](/docs/zh/api-reference/images/seedream/generation) 了解 Seedream 扩展参数
* 查看 [模型列表](/docs/zh/api-reference/models/list-models) 了解可用的图片模型


## OpenAPI

````yaml zh/api-reference/images/openai/edit/openapi.json POST /v1/images/edits
openapi: 3.0.1
info:
  title: 默认模块
  description: ''
  version: 1.0.0
servers: []
security: []
tags: []
paths:
  /v1/images/edits:
    post:
      tags: []
      summary: OpenAI图片编辑接口
      parameters:
        - name: Authorization
          in: header
          description: API Key token (Bearer sk-xxx)
          required: true
          example: 'Bearer '
          schema:
            type: string
        - name: Content-Type
          in: header
          description: ''
          example: application/json
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              description: ''
              type: object
              properties:
                images:
                  type: array
                  items:
                    $ref: '#/components/schemas/ImageReference'
                    description: |-
                      图片引用 DTO。
                      用于图像编辑请求中引用输入图片，
                      支持通过 URL 地址或文件 ID 引用图片资源。
                  description: 待编辑的源图片列表
                prompt:
                  type: string
                  description: 编辑描述提示词，描述对图片的修改内容
                background:
                  type: string
                  description: 背景图像数据（用于 Outpainting 等扩展场景）
                input_fidelity:
                  type: string
                  description: 输入图片保真度控制，取值 high, low
                mask:
                  $ref: '#/components/schemas/ImageReference'
                  description: 遮罩图片，用于指定编辑区域（Inpainting）
                model:
                  type: string
                  description: 模型名称，如 "dall-e-2"
                'n':
                  type: integer
                  description: 生成图片数量
                output_compression:
                  type: integer
                  description: 输出压缩率
                output_format:
                  type: string
                  description: 输出图片格式，如 "png"、"jpeg"、"webp"
                partial_images:
                  type: integer
                  description: 流式返回时每次推送的中间结果图片数量
                response_format:
                  type: string
                  description: 响应格式，"url" 或 "b64_json"
                quality:
                  type: string
                  description: 图片质量，如 "standard"、"hd"
                size:
                  type: string
                  description: 生成图片尺寸，如 "1024x1024"
                stream:
                  type: boolean
                  description: 是否使用流式响应（SSE）
                user:
                  type: string
                  description: 终端用户标识，用于监控和滥用检测
              required:
                - model
                - prompt
                - images
            examples: {}
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: object
                properties:
                  created:
                    type: integer
                    description: 图片生成时间戳（Unix 秒级时间戳）
                    format: int64
                  output_format:
                    type: string
                    description: 输出图片格式回显
                  background:
                    type: string
                    description: 背景图像数据回显
                  quality:
                    type: string
                    description: 图片质量回显
                  size:
                    type: string
                    description: 生成图片尺寸回显
                  data:
                    type: array
                    items:
                      description: >-
                        单张图片数据。

                        包含图片的 URL 或 Base64 编码数据、修订后的提示词、图片尺寸等信息。

                        兼容 OpenAI DALL-E 格式（b64_json /
                        revised_prompt）及通用图像模型返回格式。
                      type: object
                      properties:
                        b64_json:
                          type: string
                          description: 图片的 Base64 编码数据（当 response_format 为 b64_json 时返回）
                        revised_prompt:
                          type: string
                          description: 模型自动修订后的提示词（DALL-E 3 特性）
                        url:
                          type: string
                          description: 图片的可公开访问 URL 地址
                    description: 生成的图片数据列表
                  usage:
                    description: Token 及图片用量统计
                    type: object
                    properties:
                      input_tokens:
                        type: integer
                        description: 输入 Token 消耗数量
                      output_tokens:
                        type: integer
                        description: 输出 Token 消耗数量
                      total_tokens:
                        type: integer
                        description: 总 Token 消耗数量
                      input_tokens_details:
                        $ref: '#/components/schemas/InputTokensDetails'
                        description: 输入 Token 明细
                      output_tokens_details:
                        $ref: '#/components/schemas/OutputTokensDetails'
                        description: 输出 Token 明细
                    required:
                      - output_tokens_details
                description: ''
          headers: {}
      deprecated: false
      security: []
components:
  schemas:
    ImageReference:
      type: object
      properties:
        image_url:
          type: string
          description: 图片的 URL 地址
        file_id:
          type: string
          description: 文件系统中的文件 ID（上传后获取）
    InputTokensDetails:
      type: object
      properties:
        image_tokens:
          type: integer
          description: 图片部分消耗的 Token 数
        text_tokens:
          type: integer
          description: 文本部分消耗的 Token 数
    OutputTokensDetails:
      type: object
      properties:
        image_tokens:
          type: integer
          description: 图片部分消耗的 Token 数
        text_tokens:
          type: integer
          description: 文本部分消耗的 Token 数

````