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

# 生成图像

> <ul><li>在给定提示的情况下创建图像。[了解更多](https://platform.openai.com/docs/guides/images)。</li></ul>

<RequestExample>
  ```bash cURL theme={null} theme={null}
  curl --request POST \
    --url 'https://api.elkapi.com/v1/images/generations' \
    --header 'Authorization: Bearer <token>' \
    --header 'Content-Type: application/json' \
    --data '{
    "prompt": "一只小猫在阳光下的花园里奔跑，电影感，高细节",
    "model": "gpt-image-1.5",
    "size": "1024x1024",
    "stream": false
  }'
  ```

  ```python Python theme={null} theme={null}
  import requests

  url = "https://api.elkapi.com/v1/images/generations"
  headers = {
      "Authorization": "Bearer <token>"
  }
  headers["Content-Type"] = "application/json"
  payload = {
      "prompt": "一只小猫在阳光下的花园里奔跑，电影感，高细节",
      "model": "gpt-image-1.5",
      "size": "1024x1024",
      "stream": False
  }

  response = requests.request("POST", url, headers=headers, json=payload)
  print(response.json())
  ```

  ```javascript JavaScript theme={null} theme={null}
  const url = "https://api.elkapi.com/v1/images/generations";

  const headers = {
    "Authorization": "Bearer <token>"
  };
  headers["Content-Type"] = "application/json";
  const payload = {
    "prompt": "一只小猫在阳光下的花园里奔跑，电影感，高细节",
    "model": "gpt-image-1.5",
    "size": "1024x1024",
    "stream": false
  };

  const response = await fetch(url, {
    method: "POST",
    headers,
    body: JSON.stringify(payload)
  });

  console.log(await response.json());
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null} theme={null}
  {
    "created": 1,
    "data": [
      {
        "b64_json": "b64_json",
        "url": "https://example.com/image.png"
      }
    ],
    "usage": {
      "total_tokens": 1,
      "input_tokens": 1,
      "output_tokens": 1,
      "input_tokens_details": {
        "text_tokens": 1,
        "image_tokens": 1
      }
    }
  }
  ```
</ResponseExample>

## Authorizations

<ParamField header="Authorization" type="string" required>
  所有接口均需要使用 Bearer Token 进行认证。

  在请求头中添加：

  ```
  Authorization: Bearer YOUR_API_KEY
  ```
</ParamField>

## Body

<ParamField body="model" type="string" default="gpt-image-1.5">
  用于图像生成的模型。`dall-e-2`、`dall-e-3` 或 `gpt-image-1` 之一。默认为 `dall-e-2`，除非使用特定于 `gpt-image-1` 的参数。

  示例：`gpt-image-1.5`
</ParamField>

<ParamField body="prompt" type="string" required>
  所需图像的文本描述。`gpt-image-1` 的最大长度为 32000 个字符，`dall-e-2` 的最大长度为 1000 个字符，`dall-e-3` 的最大长度为 4000 个字符。
</ParamField>

<ParamField body="n" type="integer">
  要生成的图像数量。必须介于 1 到 10 之间。对于 `dall-e-3`，仅支持 `n=1`。
</ParamField>

<ParamField body="size" type="string">
  生成的图像的大小。`对于 gpt-image-1`，必须是 `1024x1024`、`1536x1024`（横向）、`1024x1536`（纵向）或`自动`（默认值）之一，`对于 dall-e-2`，必须是 `256x256、``512x512` 或 `1024x1024` 之一，对于 `dall-e-3`，必须是 `1024x1024`、`1792x1024` 或 `1024x1792` 之一。
</ParamField>

<ParamField body="background" type="string">
  允许为生成的图像的背景设置透明度。此参数仅支持 `gpt-image-1`。必须是以下之一 `透明`、`不透明`或`自动`（默认值）。使用`自动`时，模型将自动确定图像的最佳背景。

  如果`是透明`的，则输出格式需要支持透明度，因此应将其设置为 `png`（默认值）或 `webp`。
</ParamField>

<ParamField body="moderation" type="string">
  控制 `gpt-image-1` 生成的图像的内容审核级别。必须为`低，` 以进行限制较少的筛选或`自动`（默认值）。
</ParamField>

<ParamField body="quality" type="string">
  将生成的图像的质量。
</ParamField>

<ParamField body="stream" type="string" />

<ParamField body="style" type="string" />

<ParamField body="user" type="string" />

## Response

<ResponseField name="created" type="integer" />

<ResponseField name="data" type="array<object>">
  <ResponseField name="data.b64_json" type="string" />

  <ResponseField name="data.url" type="string" required />
</ResponseField>

<ResponseField name="usage" type="object">
  <ResponseField name="usage.total_tokens" type="integer" required />

  <ResponseField name="usage.input_tokens" type="integer" required />

  <ResponseField name="usage.output_tokens" type="integer" required />

  <ResponseField name="usage.input_tokens_details" type="object" required>
    <ResponseField name="usage.input_tokens_details.text_tokens" type="integer" required />

    <ResponseField name="usage.input_tokens_details.image_tokens" type="integer" required />
  </ResponseField>
</ResponseField>
