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

# 图像生成

> 使用 OpenAI Images 格式生成和编辑图片。

```text theme={null}
POST https://www.jiji.cc/v1/images/generations
POST https://www.jiji.cc/v1/images/edits
```

兼容 OpenAI Images 接口，支持文生图和图片编辑。可用的图像模型和价格以 [模型广场](https://www.jiji.cc/extension/models) 为准。

<Note>
  下方示例中的 `gpt-image-2` 仅为示例，请替换为模型广场中实际提供的图像模型 ID，并确认 Key 所在分组包含该模型。
</Note>

## 文生图

<CodeGroup>
  ```python Python theme={null}
  import base64
  import os
  from openai import OpenAI

  client = OpenAI(
      base_url="https://www.jiji.cc/v1",
      api_key=os.environ["JIJI_API_KEY"],
  )

  resp = client.images.generate(
      model="gpt-image-2",
      prompt="一只戴着宇航员头盔的橘猫，漂浮在太空中，插画风格",
      size="1024x1024",
  )

  with open("cat.png", "wb") as f:
      f.write(base64.b64decode(resp.data[0].b64_json))
  ```

  ```javascript Node.js theme={null}
  import fs from "node:fs";
  import OpenAI from "openai";

  const client = new OpenAI({
    baseURL: "https://www.jiji.cc/v1",
    apiKey: process.env.JIJI_API_KEY,
  });

  const resp = await client.images.generate({
    model: "gpt-image-2",
    prompt: "一只戴着宇航员头盔的橘猫，漂浮在太空中，插画风格",
    size: "1024x1024",
  });

  fs.writeFileSync("cat.png", Buffer.from(resp.data[0].b64_json, "base64"));
  ```

  ```bash cURL theme={null}
  curl https://www.jiji.cc/v1/images/generations \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $JIJI_API_KEY" \
    -d '{
      "model": "gpt-image-2",
      "prompt": "一只戴着宇航员头盔的橘猫，漂浮在太空中，插画风格",
      "size": "1024x1024"
    }'
  ```
</CodeGroup>

返回结果中，图片以 Base64 编码放在 `data[0].b64_json` 中，解码后保存为图片文件。部分模型返回的是图片链接 `data[0].url`。

## 编辑图片

上传一张或多张图片，让模型按提示词修改：

```python Python theme={null}
resp = client.images.edit(
    model="gpt-image-2",
    image=open("cat.png", "rb"),
    prompt="把背景换成海边的日落",
)

with open("cat-beach.png", "wb") as f:
    f.write(base64.b64decode(resp.data[0].b64_json))
```

## 常用参数

| 参数 | 说明 |
| - | - |
| `model` | 图像模型 ID |
| `prompt` | 图片描述，越具体效果越好 |
| `size` | 图片尺寸，例如 `1024x1024`、`1536x1024`、`1024x1536`，以模型支持为准 |
| `n` | 生成数量 |
| `quality` | 图片质量，例如 `low`、`medium`、`high`，质量越高耗时和费用越高 |

## 使用建议

* 生图通常需要几十秒到数分钟，请把客户端的超时时间设置在 3 分钟以上
* 描述中写清主体、场景、风格、构图和光线，效果更稳定
* 在 [Cherry Studio](/clients/cherry-studio) 等客户端中也可以直接使用图像模型


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.