CLI 输出文件转图片
Find the Image Your CLI Agent Just Wrote, and Load It Back
- image
- output_path
Every "let an external CLI do the heavy lifting" workflow needs a way back in. KSCLIImageOutputFile is the return trip: it takes the stdout, stderr, and exit code from KSAntigravityCLIExecute, plus the output_path that KSCLIImageInputFiles predicted, and loads the generated image back into the graph as a tensor. It's the node that closes the loop.
How it finds the image
Three fallbacks, in order, and it's a nicely defensive design:
- The expected path. If
expected_output_pathpoints at an existing image file, it loads that. In the intended pipeline this isoutput_pathfromKSCLIImageInputFiles- the file the agent was told to write. This is the fast path that hits 90% of the time. - Parse stdout/stderr. If the expected path doesn't exist, it regex-scans the combined stdout+stderr for anything that looks like an image path (
.png,.jpg,.jpeg,.webp,.bmp,.tif,.tiff, quoted or not) and tries those, resolved againstworking_directoryif relative. This covers agents that ignore your instructions and just "print only the saved image path" somewhere in their output - which is why the default Antigravity prompt says to do exactly that. - Search a directory. Last resort: if
search_directoryis set, it loads the most-recently-modified image in that folder (recursively). Crude but effective - the newest file is usually the one your agent just made.
If all three fail, it raises - and if return_code was non-zero, the error is prefixed with "CLI failed with exit code N" plus the first chunk of output. That distinction is useful: exit 0 with no image means the agent finished but didn't save where expected; non-zero means the agent itself crashed.
The inputs that matter
stdout/stderr/return_code- straight from the CLI execute node. All three are required.expected_output_path- the predicted save location. Required, but if it's blank the node skips straight to parsing.working_directory(optional) - used to resolve relative image paths. Blank = ComfyUI's current working directory.search_directory(optional) - the last-resort folder to scan.
Outputs: image (the tensor, ready for preview, saving, or more processing) and output_path (the absolute path of whatever it actually loaded, so you can confirm which file you got).
Installing
With the pack:
cd ComfyUI/custom_nodes
git clone https://github.com/kongshan4219/ComfyUI-Kongshan-Nodes
restart ComfyUI. No keys, no models - pure local file handling.
Gotchas
- The same numeric
working_directoryquirk as its sibling. The source warns that a widget value mismatch can show up as a numericworking_directory; the node detects it, prints a warning, and falls back to the current directory. If paths keep resolving wrong, check whether yourworking_directorywire actually connected. - It loads RGB only. The internal loader converts to RGB before returning a tensor, so a transparent PNG comes back with its alpha dropped. For this pipeline's white-background output that's fine; if you need alpha, this isn't the node.
- Wrong-file risk in fallback 3. "Newest file in the folder" is a guess. If your agent wrote several images, you may get the wrong one - check the
output_pathoutput before trusting the tensor.
The whole CLI trio (input-files → execute → output-file) is the most unusual thing in this pack: a full product-photo pipeline that passes through an autonomous coding agent. It's young, unknown (zero impressions everywhere as of now), and a little janky - but when it works, it's a genuinely different way to drive ComfyUI.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| stdout | STRING | CLI 标准输出。节点会从中解析可能的图片路径,常用于工具打印保存路径的场景。 | |
| stderr | STRING | CLI 标准错误。期望路径找不到时也会从这里解析图片路径,并在失败时展示错误片段。 | |
| return_code | INT | CLI 退出码。非 0 且找不到图片时节点会报 CLI failed。 | |
| expected_output_path | STRING | 首选图片路径,通常来自 CLI 图片输入转临时文件节点的 output_path。找到后直接加载。 | |
| working_directoryopt | STRING | 解析相对图片路径时使用的工作目录。留空使用当前 Python 工作目录。 | |
| search_directoryopt | STRING | 兜底搜索目录。期望路径和输出文本都找不到图片时,节点会加载该目录中修改时间最新的图片。 |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| output_path | STRING | — |