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

# GPT Image

参数及使用方式已对齐官方，详细参数可直接参考官方文档。

## 调用方式

* `https://{api_domain}/openai/v1/images/generations`
* `https://{api_domain}/openai/v1/images/edits`

BaseURL：

* `https://{api_domain}/openai/v1`

## 支持模型

* `gpt-image-2`
* `gpt-image-2-oai`
* `gpt-image-2.5-sunburst-oai`
* `gpt-image-2.5-flare-oai`

## 说明

该接口兼容 OpenAI Image API，当前支持图片生成和图片编辑。详细参数以 OpenAI 官方文档为准：

* [OpenAI 官方图片生成指南](https://developers.openai.com/api/docs/guides/image-generation)

* [Images generate](https://developers.openai.com/api/reference/resources/images/methods/generate)

* [Images edit](https://developers.openai.com/api/reference/resources/images/methods/edit)

* 图片生成请求使用 `application/json`。

* 图片编辑支持 `multipart/form-data` 文件上传，也支持 `application/json`，使用 `images` 数组传入图片引用；下方提供 JSON 示例。

* 建议显式设置 `size` 和 `quality`，便于控制图片质量与成本。费用按实际 Token 用量及模型价格计算，用量可查看响应中的 `usage` 字段。

## 模型与参数

* 使用 `gpt-image-2.5-sunburst-oai` 进行精细图片编辑，或使用 `gpt-image-2.5-flare-oai` 进行快速图片生成。请求中的 `model` 请使用上方列出的完整名称。
* 2.5 的 `quality`：`low` / `medium` / `high` / `xhigh` / `max` / `auto`，默认 `auto`；GPT Image 2 不支持新增的 `xhigh` / `max`。
* 生成透明背景图片时，设置 `background="transparent"`，并使用 `output_format="png"` 或 `"webp"`。
* 通过 `size` 设置图片尺寸，例如 `1024x1024`、`1536x1024` 或 `1024x1536`。自定义尺寸的约束及完整参数说明请参考[官方文档](https://developers.openai.com/api/docs/guides/image-generation)。

## REST API

### 1. 图片生成

```bash theme={null}
curl -X POST 'https://{api_domain}/openai/v1/images/generations' \
  -H 'Authorization: Bearer apikey' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-image-2.5-flare-oai",
    "prompt": "A product photo of a white ceramic mug on a wooden table",
    "size": "1024x1024",
    "quality": "high",
    "output_format": "png",
    "n": 1
  }' | jq -r '.data[0].b64_json' | base64 --decode > output.png
```

### 2. 图片编辑

```bash theme={null}
curl -X POST 'https://{api_domain}/openai/v1/images/edits' \
  -H 'Authorization: Bearer apikey' \
  -F 'model=gpt-image-2' \
  -F 'image[]=@input.png' \
  -F 'mask=@mask.png' \
  -F 'prompt=Replace the background with a clean studio backdrop' \
  -F 'size=1024x1024' \
  -F 'quality=high' \
  -F 'output_format=png' \
  | jq -r '.data[0].b64_json' | base64 --decode > edited.png
```

多图参考编辑时可以重复传 `image[]`：

```bash theme={null}
curl -X POST 'https://{api_domain}/openai/v1/images/edits' \
  -H 'Authorization: Bearer apikey' \
  -F 'model=gpt-image-2' \
  -F 'image[]=@body-lotion.png' \
  -F 'image[]=@bath-bomb.png' \
  -F 'image[]=@soap.png' \
  -F 'prompt=Create a photorealistic gift basket using the reference products' \
  -F 'size=1024x1024' \
  -F 'quality=high'
```

### 3. 图片编辑（JSON）

将 `{base64_image}` 替换为输入 PNG 图片的 Base64 编码。

```bash theme={null}
curl -X POST 'https://{api_domain}/openai/v1/images/edits' \
  -H 'Authorization: Bearer {your-api-key}' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-image-2.5-sunburst-oai",
    "images": [
      {"image_url": "data:image/png;base64,{base64_image}"}
    ],
    "prompt": "Change the shape to green. Preserve the simple composition.",
    "size": "1024x1024",
    "quality": "xhigh",
    "moderation": "low",
    "output_format": "png"
  }'
```

## OpenAI SDK

### Python

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

api_key = "{your-api-key}"

client = OpenAI(
    api_key=api_key,
    base_url="https://{api_domain}/openai/v1",
)

result = client.images.generate(
    model="gpt-image-2.5-flare-oai",
    prompt="A product photo of a white ceramic mug on a wooden table",
    size="1024x1024",
    quality="high",
)

image_base64 = result.data[0].b64_json
with open("output.png", "wb") as f:
    f.write(base64.b64decode(image_base64))
```

图片编辑：

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

api_key = "{your-api-key}"

client = OpenAI(
    api_key=api_key,
    base_url="https://{api_domain}/openai/v1",
)

result = client.images.edit(
    model="gpt-image-2",
    image=open("input.png", "rb"),
    mask=open("mask.png", "rb"),
    prompt="Replace the background with a clean studio backdrop",
    size="1024x1024",
    quality="high",
)

image_base64 = result.data[0].b64_json
with open("edited.png", "wb") as f:
    f.write(base64.b64decode(image_base64))
```

### Node.js

```javascript theme={null}
import OpenAI from "openai";
import fs from "fs";

const client = new OpenAI({
  apiKey: "{your-api-key}",
  baseURL: "https://{api_domain}/openai/v1",
});

const result = await client.images.generate({
  model: "gpt-image-2.5-flare-oai",
  prompt: "A product photo of a white ceramic mug on a wooden table",
  size: "1024x1024",
  quality: "high",
});

const imageBase64 = result.data[0].b64_json;
fs.writeFileSync("output.png", Buffer.from(imageBase64, "base64"));
```

图片编辑：

```javascript theme={null}
import OpenAI, { toFile } from "openai";
import fs from "fs";

const client = new OpenAI({
  apiKey: "{your-api-key}",
  baseURL: "https://{api_domain}/openai/v1",
});

const result = await client.images.edit({
  model: "gpt-image-2",
  image: await toFile(fs.createReadStream("input.png"), null, {
    type: "image/png",
  }),
  mask: await toFile(fs.createReadStream("mask.png"), null, {
    type: "image/png",
  }),
  prompt: "Replace the background with a clean studio backdrop",
  size: "1024x1024",
  quality: "high",
});

const imageBase64 = result.data[0].b64_json;
fs.writeFileSync("edited.png", Buffer.from(imageBase64, "base64"));
```

## 最佳实践

### moderation 参数

* `moderation="auto"` 为默认标准过滤；`moderation="low"` 表示更宽松的过滤。
* `moderation` 可用于图片生成和图片编辑请求。
