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

# 图像生成与编辑 API

> 根据你的工作流选择 CometAPI 图像路由，包括兼容 OpenAI 的生成与编辑、Gemini 和 Nano Banana、Midjourney、Flux、Bria、Replicate，以及 Seedream 或 SeedEdit 工作流。

通过选择与工作流匹配的请求格式来使用 CometAPI 图像模型文档。兼容 OpenAI 的图像端点涵盖常见的生成和编辑流程；特定提供商页面则涵盖基于任务的图像系统。

## 选择图像 API

<CardGroup cols={2}>
  <Card title="创建图像" icon="image" href="/api/image/openai/images">
    使用兼容 OpenAI 的请求生成图像。
  </Card>

  <Card title="创建图像编辑" icon="sparkles" href="/api/image/openai/image-editing">
    使用兼容 OpenAI 的请求编辑图像。
  </Card>

  <Card title="使用 Gemini 生成图像" icon="image" href="/api/image/gemini/gemini-generates-image">
    使用 Gemini 格式生成或编辑图像。
  </Card>

  <Card title="开始使用 Midjourney API" icon="sparkles" href="/api/image/midjourney/midjourney-api-quick-start">
    提交并轮询 Midjourney 图像任务。
  </Card>

  <Card title="生成 Flux 图像" icon="image" href="/api/image/flux/flux-generate-image">
    通过 CometAPI 生成 Flux 图像。
  </Card>

  <Card title="生成 Bria 图像" icon="image" href="/api/image/bria/generate-image">
    使用 Bria 图像生成工具。
  </Card>

  <Card title="创建 Replicate 预测" icon="sparkles" href="/api/image/replicate/create-predictions-general">
    运行 Replicate 格式的图像预测请求。
  </Card>

  <Card title="创建 Seedream 图像" icon="image" href="/api/image/seededit-seedream/bytedance-image-generation">
    创建 Seedream 图像生成请求。
  </Card>
</CardGroup>

## 生成图像

使用来自[模型页面](/zh-Hans/overview/models)或[模型目录](https://www.cometapi.com/models/)的支持图像的 model ID。下面的示例调用的是兼容 OpenAI 的“创建图像”端点。

<Note>
  这些示例使用占位符 `your-image-model-id`。在运行请求之前，请将其替换为[模型页面](/zh-Hans/overview/models)或[模型目录](https://www.cometapi.com/models/)中的可用图像 model ID。
</Note>

<Tip>
  打开[创建图像](/api/image/openai/images)以使用 playground 和端点 schema。
</Tip>

<CodeGroup>
  ```python Python theme={null}
  import os
  import requests

  response = requests.post(
      "https://api.cometapi.com/v1/images/generations",
      headers={
          "Authorization": "Bearer " + os.environ["COMETAPI_KEY"],
          "Content-Type": "application/json",
      },
      json={
          "model": "your-image-model-id",
          "prompt": "A clean product photo of a glass teapot on a white table",
          "size": "1024x1024",
      },
      timeout=120,
  )

  response.raise_for_status()
  result = response.json()
  print(result["data"][0].keys())
  ```

  ```javascript Node.js theme={null}
  const response = await fetch("https://api.cometapi.com/v1/images/generations", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "your-image-model-id",
      prompt: "A clean product photo of a glass teapot on a white table",
      size: "1024x1024",
    }),
  });

  if (!response.ok) {
    throw new Error(await response.text());
  }

  const result = await response.json();
  console.log(Object.keys(result.data[0]));
  ```

  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/images/generations \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "your-image-model-id",
      "prompt": "A clean product photo of a glass teapot on a white table",
      "size": "1024x1024"
    }'
  ```
</CodeGroup>

## 响应示例

成功响应可能如下所示。根据所选模型，每个条目可以包含 base64 图像或结果 URL：

```json theme={null}
{
  "created": 1779872000,
  "background": "opaque",
  "data": [
    {
      "b64_json": "iVBORw0KGgo..."
    }
  ],
  "output_format": "png",
  "quality": "low",
  "size": "1024x1024",
  "usage": {
    "input_tokens": 19,
    "input_tokens_details": {
      "image_tokens": 0,
      "text_tokens": 19
    },
    "output_tokens": 196,
    "output_tokens_details": {
      "image_tokens": 196,
      "text_tokens": 0
    },
    "total_tokens": 215
  }
}
```

## 示例模型记录

<Info>
  此示例模型目录响应展示了 `/api/models` 的响应包结构以及一条图像模型记录的形态。它并不是完整的模型列表。
</Info>

```bash cURL theme={null}
curl https://api.cometapi.com/api/models
```

```json theme={null}
{
  "success": true,
  "page": 1,
  "page_size": 20,
  "total": 302,
  "data": [
    {
      "created": 1776391310,
      "id": "your-image-model-id",
      "code": "your-image-model-id",
      "provider": "ExampleProvider",
      "provider_code": "example",
      "name": "Example image model",
      "model_type": "image",
      "features": [
        "text-to-image"
      ],
      "endpoints": "{\n  \"openai-image\": {\n    \"path\": \"/v1/images/generations\",\n    \"method\": \"POST\"\n  }\n}",
      "pricing": {
        "currency": "USD / M Tokens",
        "input": 4,
        "output": 24,
        "per_request": null,
        "per_second": null
      }
    }
  ]
}
```

## 常见错误

<AccordionGroup>
  <Accordion title="Invalid model ID">
    从[模型页面](/zh-Hans/overview/models)选择一个支持图像的模型。
  </Accordion>

  <Accordion title="Unsupported size">
    使用所选图像端点支持的尺寸。
  </Accordion>

  <Accordion title="Expired result URL">
    在提供商链接过期前下载已生成的资源。
  </Accordion>

  <Accordion title="Upload too large">
    使用图像 URL 或更小的文件，而不是大型 base64 负载。
  </Accordion>

  <Accordion title="Missing image data">
    同时检查 `b64_json` 和 `url` 字段，因为输出形态可能因模型而异。
  </Accordion>
</AccordionGroup>

## 错误代码和重试策略

<AccordionGroup>
  <Accordion title="400">
    在修复 prompt、尺寸或图像输入之前，不要重试。
  </Accordion>

  <Accordion title="401">
    在 API key 已提供且有效之前，不要重试。
  </Accordion>

  <Accordion title="404">
    重试前请检查 base URL、路径和 model ID。
  </Accordion>

  <Accordion title="413">
    缩小上传大小后再重试。
  </Accordion>

  <Accordion title="429">
    使用指数退避进行重试，并降低并发度。
  </Accordion>

  <Accordion title="500 or 503">
    对临时性的提供商或服务错误使用退避重试。
  </Accordion>
</AccordionGroup>

<Tip>
  有关实现模式，请参阅[错误代码和重试策略](/zh-Hans/guides/error-codes-and-retry-strategy)和[速率限制与并发](/zh-Hans/guides/rate-limits-and-concurrency)。
</Tip>

## 定价和模型目录

<CardGroup cols={3}>
  <Card title="Models page" icon="list" href="/overview/models">
    了解 CometAPI 如何在文档中公开 model ID。
  </Card>

  <Card title="Model directory" icon="puzzle-piece" href="https://www.cometapi.com/models/">
    浏览模型可用性和能力。
  </Card>

  <Card title="Pricing" icon="tag" href="https://www.cometapi.com/pricing/">
    调用模型前先检查定价。
  </Card>
</CardGroup>
