Nodes/Kongshan Nodes/CLI 输出文件转图片
ComfyUI Node

CLI 输出文件转图片

Find the Image Your CLI Agent Just Wrote, and Load It Back

By kongshan4219·Created 3 months ago·Updated 3 months ago· 0
CLI 输出文件转图片
    • image
    • output_path
    stdout
    stderr
    return_code
    expected_output_path
    working_directory
    search_directory

    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:

    1. The expected path. If expected_output_path points at an existing image file, it loads that. In the intended pipeline this is output_path from KSCLIImageInputFiles - the file the agent was told to write. This is the fast path that hits 90% of the time.
    2. 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 against working_directory if 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.
    3. Search a directory. Last resort: if search_directory is 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_directory quirk as its sibling. The source warns that a widget value mismatch can show up as a numeric working_directory; the node detects it, prints a warning, and falls back to the current directory. If paths keep resolving wrong, check whether your working_directory wire 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_path output 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.

    CategoryKongshan/CLI

    Inputs (6)

    NameTypeDefaultDescription
    stdoutSTRINGCLI 标准输出。节点会从中解析可能的图片路径,常用于工具打印保存路径的场景。
    stderrSTRINGCLI 标准错误。期望路径找不到时也会从这里解析图片路径,并在失败时展示错误片段。
    return_codeINTCLI 退出码。非 0 且找不到图片时节点会报 CLI failed。
    expected_output_pathSTRING首选图片路径,通常来自 CLI 图片输入转临时文件节点的 output_path。找到后直接加载。
    working_directoryoptSTRING解析相对图片路径时使用的工作目录。留空使用当前 Python 工作目录。
    search_directoryoptSTRING兜底搜索目录。期望路径和输出文本都找不到图片时,节点会加载该目录中修改时间最新的图片。

    Outputs (2)

    NameTypeDescription
    imageIMAGE
    output_pathSTRING