# imagegen

> Agent 用來依提示詞生成圖片（可附參考圖片），並存成本機檔案的指令。

Source: https://air.build/tw/docs/cli/imagegen/

`imagegen` 會將文字提示詞轉成一或多個圖片檔。你可以加入參考圖片（本機檔案或網址）供模型參考。它會等待圖片完成、儲存檔案並印出路徑。

## 執行位置

雲端團隊與 Mac 團隊中的 Agent 都能使用同一個指令。登入與 Agent 歸屬資訊來自 Agent 的環境，因此無須任何設定。此指令需要 `curl`、`jq` 與 `base64`，Agent 的環境已提供。兩種團隊的差異請參閱[選擇 Agent 的執行位置](https://air.build/tw/docs/get-started/where-agents-run/)。

## 語法

```text
imagegen [options] PROMPT
imagegen [options] --prompt-file PATH
imagegen --help
```

提示詞是所有非選項的字詞以空格連接而成，因此不一定要加引號。`--` 之後的所有內容都會視為提示詞，即使是以 `-` 開頭的字詞也一樣。

## 選項

所有需要值的選項也接受 `--option=value` 的寫法。

| 選項 | 值 | 預設 | 意義 |
| --- | --- | --- | --- |
| `-i`, `--image` | 路徑或 `http(s)://` 網址 | 無 | 加入參考圖片。重複使用可加入多張。本機檔案必須是 PNG、JPEG、WebP 或 GIF（依副檔名判斷：`.png`、`.jpg`、`.jpeg`、`.webp`、`.gif`），且不得為空。 |
| `-n`, `--count` | 1 到 10 的整數 | `1` | 要生成的圖片數量。 |
| `-o`, `--output` | 路徑 | `$TMPDIR` 下的新資料夾 | 一張圖片時：要寫入的確切檔案。多張時：要寫入的資料夾。 |
| `--model` | 模型 id，不含空格 | 服務的預設值 | 要使用的圖片模型。 |
| `--size` | 尺寸或長寬比簡寫，不含空格 | `auto` | 輸出尺寸。原樣傳給模型。 |
| `--quality` | `auto`, `low`, `medium`, `high` | `auto` | 輸出品質。 |
| `--format` | `png`, `jpeg`, `webp` | `png` | 要求的檔案格式。 |
| `--background` | `auto`, `transparent`, `opaque` | `auto` | 背景。`transparent` 需要 `png` 或 `webp`。 |
| `--prompt-file` | UTF-8 文字檔的路徑 | 無 | 從檔案讀取提示詞。此選項與指令列上的提示詞只能擇一。 |
| `-h`, `--help` | — | — | 印出說明。 |

## 檔案存放位置

| `--output` | `--count` | 結果 |
| --- | --- | --- |
| 未指定 | 任意 | 新資料夾 `$TMPDIR/imagegen.XXXXXX/`（未設定 `TMPDIR` 時位於 `/tmp` 下），檔案為 `image-1.png`、`image-2.png`… |
| 檔案路徑 | `1` | 該確切檔案。不存在的上層資料夾會自動建立。 |
| 資料夾路徑 | 大於 `1` | 該資料夾中的 `image-1.<ext>`、`image-2.<ext>`…，資料夾必要時會自動建立。 |

副檔名依服務傳回的格式而定（`png`、`jpeg`、`webp`、`gif` 或 `svg`），否則採用 `--format`。`imagegen` 絕不覆寫檔案：若目標檔案已存在，會停止並顯示錯誤。未指定 `--output` 時，不會寫入任何檔案到專案資料夾；請自行將要保留的檔案複製到專案中。

## 輸出

成功時，stdout 每行一個絕對檔案路徑，沒有其他內容，因此可以導向管線：

```text
/tmp/imagegen.Ab12Cd/image-1.png
/tmp/imagegen.Ab12Cd/image-2.png
```

進度會輸出到 stderr：請求前顯示 `Generating 2 images with <model>...`，結束時顯示 `Generated 2 images.`。

## 結束代碼與錯誤

所有錯誤都會輸出到 stderr，並以 `Error:` 開頭。用法錯誤還會加上 `Usage: imagegen [options] PROMPT` 與 `Run imagegen --help for details.`。

| 結束代碼 | 意義 |
| --- | --- |
| `0` | 圖片已儲存；路徑輸出在 stdout。 |
| `1` | 請求已送達服務但失敗，或結果無法儲存。 |
| `2` | 指令列或輸入有誤，或環境缺少某些項目。未生成任何內容。 |

| 訊息 | 結束代碼 | 處理方式 |
| --- | --- | --- |
| `a prompt is required.` / `the prompt cannot be empty.` | 2 | 傳入提示詞或 `--prompt-file`。 |
| `pass either PROMPT or --prompt-file, not both.` | 2 | 只保留其中一個。 |
| `prompt file is not readable: PATH` | 2 | 檢查路徑。 |
| `--count must be an integer from 1 to 10.` | 2 | 使用 1–10。 |
| `--quality must be auto, low, medium, or high.` | 2 | 使用其中一個值。 |
| `--format must be png, jpeg, or webp.` | 2 | 使用其中一個值。 |
| `--background must be auto, transparent, or opaque.` | 2 | 使用其中一個值。 |
| `transparent backgrounds require png or webp output.` | 2 | 加上 `--format png` 或 `--format webp`。 |
| `--model must be a nonempty model id without whitespace.` / `--size must be a nonempty value without whitespace.` | 2 | 修正該值。 |
| `reference image is not readable: PATH` / `reference image is empty: PATH` | 2 | 檢查檔案。 |
| `unsupported reference image type: PATH` | 2 | 使用 `.png`、`.jpg`、`.jpeg`、`.webp` 或 `.gif` 檔案。 |
| `prompt and reference images exceed the 20 MiB request limit.` | 2 | 減少參考圖片數量或縮小檔案，或改以網址傳入。 |
| `output already exists: PATH` | 2 | 選擇其他 `--output`，或移走既有檔案。 |
| `multi-image output must be a directory: PATH` | 2 | `--count` 大於 1 時，`--output` 必須是資料夾。 |
| `unknown option: …` | 2 | 對照上方表格檢查拼字。 |
| `the Agent runtime is missing the required '…' command.`，或表示 Agent 執行環境未提供指令所需項目的訊息 | 2 / 1 | 指令是在 AirBuild Agent 的環境之外執行。請從 Agent 執行。 |
| `could not reach the Airbuild image service.` | 1 | 網路問題。再試一次。 |
| 服務本身的訊息，或 `the Airbuild image service returned HTTP <status>.` | 1 | 閱讀訊息；它會說明服務拒絕了什麼，例如提示詞或某個值。 |
| `Airbuild returned no generated images.` / `requested N image(s), but Airbuild returned M.` | 1 | 再試一次，或修改提示詞。 |
| `could not download generated image N.` / `generated image N is empty.` / `Airbuild returned invalid base64 for image N.` / `Airbuild returned no usable data for image N.` | 1 | 再試一次。 |

## 範例

```bash
# One image, saved to a new folder under $TMPDIR
imagegen "A friendly robot app icon, flat vector style"

# Polish a sketch, saved to an exact file
imagegen --image sketch.png --output final.png "Polish this sketch"

# Three variants into one folder
imagegen --count 3 --output variants "Three calm onboarding illustrations"

# A transparent PNG with a long prompt kept in a file
imagegen --background transparent --format png --prompt-file prompt.txt -o Assets/badge.png
```
