# imagegen

> 智能体用来根据提示词生成图片的命令，可附加参考图，生成的图片保存为本地文件。

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

`imagegen` 把文字提示词变成一个或多个图片文件。你可以添加参考图（本地文件或网址）供模型参考。它会等待图片生成完毕，保存下来并输出文件路径。

## 在哪里运行

云端团队和 Mac 团队的智能体都能使用同一个命令。登录凭据和智能体归属信息都来自智能体的运行环境，无需任何设置。命令依赖 `curl`、`jq` 和 `base64`，智能体的运行环境已经提供。两种团队的区别见[选择智能体的运行位置](https://air.build/cn/docs/get-started/where-agents-run/)。

## 用法概要

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

提示词是所有非选项的词用空格连接而成，所以加不加引号都行。`--` 之后的所有内容都当作提示词，即使以 `-` 开头。

## 选项

所有带值的选项也都接受 `--option=value` 写法。

| Option | 取值 | 默认值 | 含义 |
| --- | --- | --- | --- |
| `-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.`

| Exit code | 含义 |
| --- | --- |
| `0` | 图片已保存，路径在 stdout 上。 |
| `1` | 请求已到达服务但失败，或结果无法保存。 |
| `2` | 命令行或输入有误，或运行环境缺少某些东西。没有生成任何内容。 |

| Message | Exit | 处理方法 |
| --- | --- | --- |
| `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.`，或提示智能体运行环境没有提供命令所需内容的消息 | 2 / 1 | 命令是在 AirBuild 智能体的运行环境之外运行的。请由智能体来运行。 |
| `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
```
