# gpt-image-1-mini API

本文介绍 `gpt-image-1-mini` 模型调用 API 的输入输出参数，供您使用接口时查阅字段含义。

---

## 请求参数

### 请求体

| 字段名              | 类型   | 是否必须 | 默认值        | 描述                                                                                                                       |
| ------------------- | ------ | -------- | ------------- | -------------------------------------------------------------------------------------------------------------------------- |
| prompt              | string | 必须     | -             | 提示词                                                                                                                     |
| model               | string | 必须     | -             | 本次请求使用的模型名称，此处为 `gpt-image-1-mini`。                                                                        |
| n                   | int    | 可选     | 1             | 生成图片数量，取值范围为 1~10                                                                                              |
| size                | string | 可选     | `1024x1536`   | 分辨率。支持 `1024x1024`、`1024x1536`、`1536x1024`。                                                                      |
| quality             | string | 可选     | -             | 图片质量，支持 `low`、`medium`、`high`；质量越高耗时越长。                                                                  |
| background          | string | 可选     | `auto`        | 背景透明度，支持 `transparent`（透明）、`opaque`（不透明）、`auto`（自动）。设为 `transparent` 时需配合 `output_format=png`。|
| moderation          | string | 可选     | `auto`        | 内容审核级别，支持 `auto`（默认）、`low`（宽松）。                                                                          |
| output_format       | string | 可选     | `png`         | 输出图片格式，支持 `png`、`jpeg`。                                                                                          |
| output_compression  | int    | 可选     | -             | 图片压缩强度，取值 0~100；`0` 为不压缩，`100` 为最大压缩。仅对 `jpeg` 格式生效。                                            |
| user                | string | 可选     | -             | 终端用户标识符，透传给上游用于滥用检测，不影响生成结果。                                                                    |

## 响应参数

| 字段名                                    | 类型      | 描述                                                                                                                                                                                                                                                    |
| ----------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| created                                   | `integer` | 本次请求创建时间的 Unix 时间戳（秒）。                                                                                                                                                                                                                  |
| background                                | `string`  | 本次生成图片的背景类型，与请求参数 `background` 对应，如 `opaque`、`transparent`。                                                                                                                                                                     |
| output_format                             | `string`  | 本次生成图片的格式，与请求参数 `output_format` 对应，如 `png`、`jpeg`。                                                                                                                                                                                 |
| quality                                   | `string`  | 本次生成图片的质量，与请求参数 `quality` 对应，如 `low`、`medium`、`high`。                                                                                                                                                                             |
| size                                      | `string`  | 本次生成图片的分辨率，与请求参数 `size` 对应，如 `1024x1024`。                                                                                                                                                                                          |
| data                                      | `array`   | 输出图像的信息，gpt-image-1-mini 模型返回的是 base64 数据。<br/>• `b64_json`：base64 编码的图片数据，解码后即为原始图片文件。                                                                                                                           |
| usage                                     | `object`  | 本次请求的 token 用量。                                                                                                                                                                                                                                 |
| usage.input_tokens                        | `integer` | 输入消耗的 token 总数。                                                                                                                                                                                                                                 |
| usage.input_tokens_details.text_tokens    | `integer` | 输入中文字 prompt 消耗的 token 数。                                                                                                                                                                                                                     |
| usage.input_tokens_details.image_tokens   | `integer` | 输入中图片消耗的 token 数（图片编辑接口传入图片时有值，纯文字生图时为 0）。                                                                                                                                                                             |
| usage.output_tokens                       | `integer` | 输出消耗的 token 总数。                                                                                                                                                                                                                                 |
| usage.output_tokens_details.image_tokens  | `integer` | 输出图片消耗的 token 数。                                                                                                                                                                                                                               |
| usage.total_tokens                        | `integer` | 本次请求消耗的 token 总数。                                                                                                                                                                                                                             |
| error                                     | `Object`  | 错误信息对象                                                                                                                                                                                                                                            |
| error.code                                | `string`  | 错误码                                                                                                                                                                                                                                                  |
| error.message                             | `string`  | 错误提示信息                                                                                                                                                                                                                                            |
| error.param                               | `string`  | 请求 id                                                                                                                                                                                                                                                 |

## 示例

### OPENAI 兼容接口

`POST https://api.modelverse.cn/v1/images/generations`

#### 同步请求

#### curl

```bash
curl --location 'https://api.modelverse.cn/v1/images/generations' \
  --header "Authorization: Bearer $MODELVERSE_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "gpt-image-1-mini",
    "prompt": "a beautiful flower",
    "size": "1024x1024",
    "quality": "high",
    "output_format": "png",
    "output_compression": 100
  }'
```

#### python

```python
import os, base64
from openai import OpenAI

client = OpenAI(
    base_url="https://api.modelverse.cn/v1",
    api_key=os.getenv("MODELVERSE_API_KEY", "YOUR_API_KEY")
)

res = client.images.generate(
    model="gpt-image-1-mini",
    prompt="a beautiful flower",
    size="1024x1024",
    quality="high",
)

# gpt-image-1-mini 返回 base64 数据
image_b64 = res.data[0].b64_json
raw = image_b64.split(",")[-1] if image_b64.startswith("data:") else image_b64
with open("image.png", "wb") as f:
    f.write(base64.b64decode(raw))
print("Saved to image.png")
```


### 图片编辑

`POST https://api.modelverse.cn/v1/images/edits`

使用 multipart/form-data 传参，至少包含待编辑图片 `image`（可选 `mask`）、以及 `model`、`prompt` 等字段；其余参数如 `size`、`quality`、`output_format`、`output_compression` 与生成接口一致。

> **mask 说明**：mask 为可选参数，用于指定图片中需要编辑的区域（透明区域=需要编辑，不透明区域=保留）。使用时需满足以下两个条件：
> 1. 必须为 **PNG 格式且包含 alpha 通道（RGBA 模式）**，普通 RGB 的 PNG 或 JPEG 不支持；
> 2. 尺寸必须与原图 `image` **完全一致**。

#### curl

```bash
curl --location 'https://api.modelverse.cn/v1/images/edits' \
  --header "Authorization: Bearer $MODELVERSE_API_KEY" \
  -F "image=@/path/to/your/image.png" \
  -F "mask=@/path/to/your/mask.png" \
  -F "model=gpt-image-1-mini" \
  -F "prompt=Add a beach ball in the center" \
  -F "size=1024x1024" \
  -F "n=1" \
  -F "quality=high" \
  -F "output_format=png" \
  -F "output_compression=100"
```

#### python

```python
import os, base64, requests

url = "https://api.modelverse.cn/v1/images/edits"
headers = {"Authorization": f"Bearer {os.getenv('MODELVERSE_API_KEY', '$MODELVERSE_API_KEY')}"}

files = {
    "image": ("beach.png", open("beach.png", "rb"), "image/png"),
    # 可选：提供 mask 来限定编辑区域
    # "mask": ("mask.png", open("mask.png", "rb"), "image/png"),
}

data = {
    "model": "gpt-image-1-mini",
    "prompt": "Add a beach ball in the center",
    "size": "1024x1024",
    "n": "1",
    "quality": "high",
    "output_format": "png",
    "output_compression": "100",
}

r = requests.post(url, headers=headers, files=files, data=data)
r.raise_for_status()
resp = r.json()

image_b64 = resp["data"][0]["b64_json"]
raw = image_b64.split(",")[-1] if image_b64.startswith("data:") else image_b64
with open("edit.png", "wb") as f:
    f.write(base64.b64decode(raw))
print("Saved to edit.png")
```


### 响应

```json
{
  "created": 1748484857,
  "background": "opaque",
  "output_format": "png",
  "quality": "low",
  "size": "1024x1024",
  "data": [
    {
      "b64_json": "{image_base64_string}"
    }
  ],
  "usage": {
    "total_tokens": 280,
    "input_tokens": 8,
    "output_tokens": 272,
    "input_tokens_details": {
      "image_tokens": 0,
      "text_tokens": 8
    },
    "output_tokens_details": {
      "image_tokens": 272,
      "text_tokens": 0
    }
  }
}
```

```json
{
  "error": {
    "message": "error_message",
    "type": "error_type",
    "param": "request_id",
    "code": "error_code"
  }
}
```
