---
description: "与 OpenAI 兼容的图片生成与图片编辑接口。"
---

# 图片生成

> 端点: `POST https://oneapi.zaiwenai.com/v1/images/generations`
>
> 端点: `POST https://oneapi.zaiwenai.com/v1/images/edits`

与 OpenAI Images 接口兼容，支持文生图和图片编辑。

## 图片生成

### 请求参数

| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `model` | string | 是 | 模型名称 |
| `prompt` | string | 是 | 图片描述提示词 |
| `n` | integer | 否 | 生成数量，目前固定为 1 |
| `response_format` | string | 否 | 返回格式，目前固定为 `b64_json` |

### Curl 示例

```bash
curl https://oneapi.zaiwenai.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $YOUR_API_KEY" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "A cute cat wearing a hat"
  }'
```

### OpenAI Python SDK 示例

```python
from openai import OpenAI

client = OpenAI(
    base_url="https://oneapi.zaiwenai.com/v1",
    api_key="$YOUR_API_KEY",
)

response = client.images.generate(
    model="gpt-image-2",
    prompt="A cute cat wearing a hat",
    n=1,
    response_format="b64_json",
)

print(response.data[0].b64_json[:50] + "...")
```

### 响应格式

```json
{
  "created": 1716841200,
  "data": [
    {
      "b64_json": "iVBORw0KGgoAAAANSUhEUgAA..."
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "total_tokens": 10
  }
}
```

## 图片编辑

### 请求参数

| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `image` | file | 是 | 要编辑的图片文件 |
| `model` | string | 是 | 模型名称 |
| `prompt` | string | 是 | 编辑指令 |
| `n` | integer | 否 | 生成数量，目前固定为 1 |
| `response_format` | string | 否 | 返回格式，目前固定为 `b64_json` |

### Curl 示例

```bash
curl https://oneapi.zaiwenai.com/v1/images/edits \
  -H "Authorization: Bearer $YOUR_API_KEY" \
  -F "image=@original.png" \
  -F "model=gpt-image-2" \
  -F "prompt=Add a rainbow in the sky"
```

### OpenAI Python SDK 示例

```python
response = client.images.edit(
    model="gpt-image-2",
    image=open("original.png", "rb"),
    prompt="Add a rainbow in the sky",
    n=1,
    response_format="b64_json",
)

print(response.data[0].b64_json[:50] + "...")
```

## 计费说明

图片生成和编辑按**次数计费**，每次调用消耗固定费用（由模型配置决定）。
