> ## 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("\n狀態更新：%s，請求 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) 頁面上探索更多模型。
