# 参考图生图

**这是什么**：以参考图中的主体 / 风格为依据生成新图片（图生图 / 多图参考）。

**什么时候用**：想保留指定人物、商品或风格并改变场景 / 构图时用；传了 images 即为参考生。

## 接口地址
- POST `/v1/images/generations`
- GET `/v1/images/generations/{id}`

> 本页已按统一 API 改写：生成用 `POST /v1/images/generations`（默认同步，等图片生成完再返回；加 `"async": true` 则立即返回任务，再用 `GET /v1/images/generations/{id}` 轮询），认证用 `Authorization: Bearer <key>`，错误为 OpenAI 格式。通用说明见图片指南。

同步（默认）：服务端等到图片生成完再返回 200 与 `data[].url`；超过 110 秒仍未完成则返回 202 与任务对象和 `Location` 头，任务继续运行，不要重新提交（不会重复计费），改用 `GET /v1/images/generations/{id}` 取结果。异步（`"async": true` 或请求头 `Prefer: respond-async`）：立即返回 202 与任务对象（`status: queued`，`Retry-After: 3`），用 GET 轮询到 `status` 为 `completed` 或 `failed`。

## 请求参数
| 参数 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| model | string | 是 | 子模型：hunyuan-image-3.5-preview / hunyuan-image-3.0 |
| prompt | string | — | 图片描述（可留空，由参考图驱动） |
| images | array | 是 | 参考图 URL 数组（须公网可直连），上限见对照表 |
| aspect_ratio | string | — | 画面比例，如 1:1 / 16:9 / 9:16 |
| negative_prompt / seed / enhancePrompt | string / integer / boolean | — | 反向提示词 / 随机种子 / 自动优化提示词（可选） |
| size | string | — | `宽x高`（如 `1024x1024`）：换算成该渠道接受的最接近的宽高比；同时给了 `aspect_ratio` 时以 `aspect_ratio` 为准。nano / gemini 还接受 `auto`。 |
| async | boolean | — | `true`：立即返回 202 与任务对象，再用 GET 轮询（也可用请求头 `Prefer: respond-async`）。缺省为同步。 |

## 子模型对照表

「档位价」列单位为积分/张（按次的模型为积分/次）；(default output) = 只用缺省输出、不接受 resolution；≥N refs → 为多图参考档。

| model | 用途 | 可用素材 | 出图张数 | 档位价（积分） |
| --- | --- | --- | --- | --- |
| `hunyuan-image-3.5-preview` | 3.5 预览版，缺省输出 1344×1792 | 参考图 ≤3 张 | 1 | 2K=15 (default output) |
| `hunyuan-image-3.0` | 3.0，缺省输出 1024×1024 | 参考图 ≤3 张 | 1 | 1K=20 (default output) |

> ⚠ hunyuan-image-3.5-preview：每次 1 张；hunyuan-image-3.0：每次 1 张

`model`、`prompt`、`n`、`size`、`aspect_ratio`、`resolution`、`images`、`output_format`、`negative_prompt`、`prompt_extend`、`async` 是统一接口的通用字段，服务端按渠道换算成原生字段；表中其余字段是该渠道的原生字段，按原名放在同一个请求体里透传，并且原生字段优先于同义的统一字段。`images` 只接受 http(s) 图片 URL，不接受 base64。`response_format` 只能是 `url`。

## 请求示例
### 同步请求（OpenAI SDK）

```bash
curl https://api.novapi.ai/v1/images/generations \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "hunyuan-image-3.0",
  "prompt": "the same woman sitting in a cozy cafe by the window on a rainy day, soft light",
  "images": [
    "https://cdn.novapi.ai/kling/1791949942892_d18ee23dfa3048e097c58c9c3e32a754.png"
  ],
  "aspect_ratio": "16:9"
}'
```

```python
from openai import OpenAI

client = OpenAI(base_url="https://api.novapi.ai/v1", api_key="sk-...")

img = client.images.generate(
    model="hunyuan-image-3.0",
    prompt="the same woman sitting in a cozy cafe by the window on a rainy day, soft light",
    extra_body={
        "images": [
            "https://cdn.novapi.ai/kling/1791949942892_d18ee23dfa3048e097c58c9c3e32a754.png",
        ],
        "aspect_ratio": "16:9",
    },
)
print(img.data[0].url)
```

```ts
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.novapi.ai/v1", apiKey: process.env.API_KEY });

const img = await client.images.generate({
  "model": "hunyuan-image-3.0",
  "prompt": "the same woman sitting in a cozy cafe by the window on a rainy day, soft light",
  "images": [
    "https://cdn.novapi.ai/kling/1791949942892_d18ee23dfa3048e097c58c9c3e32a754.png"
  ],
  "aspect_ratio": "16:9"
} as OpenAI.ImageGenerateParams);
console.log(img.data?.[0]?.url);
```

### 异步请求与轮询

```bash
curl https://api.novapi.ai/v1/images/generations \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "hunyuan-image-3.0",
  "prompt": "the same woman sitting in a cozy cafe by the window on a rainy day, soft light",
  "images": [
    "https://cdn.novapi.ai/kling/1791949942892_d18ee23dfa3048e097c58c9c3e32a754.png"
  ],
  "aspect_ratio": "16:9",
  "async": true
}'
```

## 响应示例
### 响应示例（同步，200）
```json
{
  "id": "img_<task-id>",
  "object": "image.generation",
  "model": "hunyuan-image-3.0",
  "status": "completed",
  "created": <unix-seconds>,
  "completed_at": <unix-seconds>,
  "error": null,
  "data": [
    { "url": "https://..." }
  ]
}
```

### 响应示例（异步，202）
```json
{
  "id": "img_<task-id>",
  "object": "image.generation",
  "model": "hunyuan-image-3.0",
  "status": "queued",
  "created": <unix-seconds>,
  "completed_at": null,
  "error": null,
  "data": []
}
```

```bash
curl https://api.novapi.ai/v1/images/generations/img_<task-id> \
  -H "Authorization: Bearer $API_KEY"

# status: queued | in_progress | completed | failed
# completed: data[].url holds the image URL(s); failed: error.code and error.message
```

## 响应字段说明
| 字段 | 类型 | 说明 |
| --- | --- | --- |
| id | string | 任务 ID，以 `img_` 开头，用于轮询。 |
| object | string | 固定为 `image.generation`。 |
| model | string | 请求里的 model。 |
| status | string | `queued`、`in_progress`、`completed` 或 `failed`。同步 200 的响应一定是 `completed`。 |
| created | integer | 创建时间（Unix 秒），与 OpenAI Images 一致。 |
| completed_at | integer \| null | 完成时间；未完成为 null。 |
| error | object \| null | 失败时为 `{code, message}`，`code` 为 `image_generation_failed`。 |
| data | array | 完成后是 `[{ "url": "https://..." }]`，一张图一个元素（`n` 大于 1 或 Midjourney 的 4 张时有多个）；未完成为空数组。 |
