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

# POST /v1/images/generations — 图像生成

> 通过 DALL-E 等模型生成图像。兼容 OpenAI Images API。

Anyone 提供两个兼容 OpenAI Images API 的图像端点。你可以根据提示词生成新图像，或提供源图像和提示词来编辑图像。两个端点都会将请求路由到你配置的上游图像渠道——例如 DALL·E 3、Stable Diffusion 或其他服务商。使用 `Authorization` 头中的 Bearer token 认证。

***

## POST /v1/images/generations

根据文本提示词生成一张或多张图像。

### 请求体

<ParamField body="model" type="string" required>
  图像生成模型，例如 `dall-e-3` 或 `dall-e-2`。可用值取决于你配置的渠道。
</ParamField>

<ParamField body="prompt" type="string" required>
  图像的文本描述。DALL·E 3 最大 4,000 字符；DALL·E 2 最大 1,000 字符。
</ParamField>

<ParamField body="n" type="integer">
  生成图像数量。默认 `1`。DALL·E 3 仅支持 `n=1`。
</ParamField>

<ParamField body="size" type="string">
  生成图像的尺寸。可选值因模型而异：

  * DALL·E 2：`256x256`、`512x512`、`1024x1024`
  * DALL·E 3：`1024x1024`、`1024x1792`、`1792x1024`

  默认 `1024x1024`。
</ParamField>

<ParamField body="quality" type="string">
  图像质量。设为 `hd` 获得更高细节但成本更高（仅 DALL·E 3）。默认 `standard`。
</ParamField>

<ParamField body="response_format" type="string">
  返回的图像数据格式。`url`（临时托管 URL）或 `b64_json`（base64 编码的图像数据）。默认 `url`。
</ParamField>

<ParamField body="style" type="string">
  生成图像的风格。`vivid`（超写实）或 `natural`（更自然）。仅 DALL·E 3。
</ParamField>

<ParamField body="background" type="string">
  图像背景样式（上游服务商支持时）。
</ParamField>

<ParamField body="output_format" type="string">
  输出文件格式（上游服务商支持时），如 `png`、`webp`。
</ParamField>

<ParamField body="output_compression" type="integer">
  输出图像压缩级别（上游服务商支持时）。范围 `0`–`100`。
</ParamField>

<ParamField body="watermark" type="boolean">
  是否包含水印（上游服务商支持时）。
</ParamField>

### 响应

<ResponseField name="created" type="integer">
  请求处理时的 Unix 时间戳。
</ResponseField>

<ResponseField name="data" type="object[]">
  生成的图像对象数组。

  <Expandable title="属性">
    <ResponseField name="url" type="string">
      指向生成图像的 URL。`response_format` 为 `url` 时出现。
    </ResponseField>

    <ResponseField name="b64_json" type="string">
      Base64 编码的图像数据。`response_format` 为 `b64_json` 时出现。
    </ResponseField>

    <ResponseField name="revised_prompt" type="string">
      模型实际使用的提示词（经服务商改写后）。DALL·E 3 始终返回此字段。
    </ResponseField>
  </Expandable>
</ResponseField>

### 示例

<CodeGroup>
  ```bash curl theme={null}
  curl https://api.anyone.ai/v1/images/generations \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "dall-e-3",
      "prompt": "A photorealistic image of a red panda sitting on a mossy log in a misty forest",
      "n": 1,
      "size": "1024x1024",
      "response_format": "url"
    }'
  ```

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

  client = OpenAI(
      api_key="YOUR_TOKEN",
      base_url="https://api.anyone.ai/v1",
  )

  response = client.images.generate(
      model="dall-e-3",
      prompt="A photorealistic image of a red panda sitting on a mossy log in a misty forest",
      n=1,
      size="1024x1024",
  )

  print(response.data[0].url)
  ```
</CodeGroup>

***

## POST /v1/images/edits

使用文本提示词编辑已有图像。上传源图像（multipart 表单），可选包含蒙版，并提供描述所需修改的提示词。

### 请求体

此端点使用 `multipart/form-data` 编码。

<ParamField body="model" type="string" required>
  图像编辑模型，例如 `dall-e-2`。并非所有图像模型都支持编辑。
</ParamField>

<ParamField body="image" type="file" required>
  要编辑的源图像。必须为有效 PNG 文件，小于 4 MB，且为正方形。
</ParamField>

<ParamField body="prompt" type="string" required>
  描述你想要应用的编辑。
</ParamField>

<ParamField body="mask" type="file">
  可选蒙版图像。蒙版的透明区域表示应用编辑的位置。必须与 `image` 尺寸相同。
</ParamField>

<ParamField body="n" type="integer">
  生成的编辑图像数量。默认 `1`。
</ParamField>

<ParamField body="size" type="string">
  输出图像尺寸。可选：`256x256`、`512x512`、`1024x1024`。默认 `1024x1024`。
</ParamField>

<ParamField body="response_format" type="string">
  `url` 或 `b64_json`。默认 `url`。
</ParamField>

### 响应

结构同 `/v1/images/generations`。

### 示例

```bash curl theme={null}
curl https://api.anyone.ai/v1/images/edits \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -F model="dall-e-2" \
  -F image="@source.png" \
  -F mask="@mask.png" \
  -F prompt="Replace the background with a snowy mountain scene" \
  -F n=1 \
  -F size="1024x1024"
```
