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

# 文本转图像

### 如何使用 Sunra API 生成图像

要使用 Sunra API 创建图像，请向指定端点发送包含输入参数的请求。该 API 利用预训练模型根据文本提示生成图像，使您能够通过自然语言描述来生成视觉效果。

以下是使用 Sunra API 从文本生成图像的示例：

<CodeGroup>
  ```javascript Javascript theme={null}
  import { sunra } from "@sunra/client";

  const result = await sunra.subscribe("black-forest-labs/flux-kontext-max/text-to-image", {
    input: {
      prompt: '一个玻璃茶壶，里面装着盛开的花茶，放在阳光透过窗户照射的木桌上，温柔的晨光。',
      prompt_enhancer: true,
      aspect_ratio: '16:9',
      output_format: 'jpeg',
      safety_tolerance: 2
    },
    logs: true,
    onQueueUpdate: (update) => {
      console.log(update)
    },
  });
  console.log(result.data);
  console.log(result.requestId);
  ```

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

  result = sunra_client.subscribe(
      "black-forest-labs/flux-kontext-max/text-to-image",
      arguments={
          "prompt": "一个玻璃茶壶，里面装着盛开的花茶，放在阳光透过窗户照射的木桌上，温柔的晨光。",
          "prompt_enhancer": True,
          "aspect_ratio": "16:9",
          "output_format": "jpeg",
          "safety_tolerance": 2
      },
      with_logs=True,
      on_enqueue=print,
      on_queue_update=print,
  )
  print(result)
  ```

  ```java Java theme={null}
  import ai.sunra.client.*;
  import ai.sunra.client.queue.*;
  import java.util.Map;
  import com.google.gson.JsonObject;
  import java.util.function.Consumer;

  ClientConfig config = ClientConfig.builder()
      .withCredentials(CredentialsResolver.fromEnv())
      .build();

  SunraClient client = SunraClient.withConfig(config);
  Map<String, Object> input = Map.of(
      "prompt", "一个玻璃茶壶，里面装着盛开的花茶，放在阳光透过窗户照射的木桌上，温柔的晨光。",
      "prompt_enhancer", true,
      "aspect_ratio", "16:9",
      "output_format", "jpeg",
      "safety_tolerance", 2
  );

  Consumer<QueueStatus.StatusUpdate> statusUpdateHandler = update -> {
      String status = update.getStatus().toString();
      String message = String.format("\nStatus Update: %s, Request ID: %s",
          status, update.getRequestId());
      System.out.println(message);
  };

  SubscribeOptions<JsonObject> options = SubscribeOptions.<JsonObject>builder()
      .input(input)
      .resultType(JsonObject.class)
      .onQueueUpdate(statusUpdateHandler)
      .logs(true)
      .build();

  Output<JsonObject> response = client.subscribe("black-forest-labs/flux-kontext-max/text-to-image", options);
  System.out.println("完成！");
  System.out.println(response.getData());
  ```

  ```bash Curl theme={null}
  curl --request POST \
    --url https://api.sunra.ai/v1/queue/black-forest-labs/flux-kontext-max/text-to-image \
    --header "Authorization: Key $SUNRA_KEY" \
    --header "Content-Type: application/json" \
    --data '{"prompt":"一个玻璃茶壶，里面装着盛开的花茶，放在阳光透过窗户照射的木桌上，温柔的晨光。","prompt_enhancer":true,"aspect_ratio":"16:9","output_format":"jpeg","safety_tolerance":2}'
  ```
</CodeGroup>

### 如何选择模型

Sunra 提供各种图像生成模型。根据所需的风格和质量选择适合您需求的模型。

可用模型包括：

* **[black-forest-labs/flux-kontext-pro](https://sunra.ai/models/black-forest-labs/flux-kontext-pro)**：一个统一模型，在 FLUX.1 质量下提供本地编辑、生成修改和文本转图像生成。以突破性速度处理文本和图像输入，进行精确的区域编辑或完整场景转换，开创性地实现了在多次编辑轮次中保持角色一致性的迭代工作流程。
* **[black-forest-labs/flux-1.1-pro](https://sunra.ai/models/black-forest-labs/flux-1.1-pro)**：FLUX 的最佳版本，以超快速度提供最先进的图像生成性能，具有顶级的提示跟随、视觉质量、图像细节和输出多样性。
* **[google-deepmind/imagen4](https://sunra.ai/models/google-deepmind/imagen4)**：Imagen 4 生成具有精细细节、准确文本和多语言提示支持的照片级 2K 图像。

要选择模型，请在 `subscribe` 方法中指定其 ID，如所示。在[文本转图像模型](https://sunra.ai/models?modalities%5B0%5D=modality-text-image)页面探索更多模型。
