保存图片到自定义目录
Save a Batch Straight to Any Folder You Choose (or Next to the Source)
- images
- saved_paths
- saved_count
ComfyUI's default SaveImage writes into output/ with a prefix, and hunting through timestamped folders to find the batch you just made gets old fast. KSDirectorySaveImages writes wherever you tell it - an absolute folder, a folder you pick with a system dialog, or automatically a white_background subfolder right next to the source image. And it numbers the files predictably: white_background_00001.png, ..._00002.png, continuing from whatever's already there. No collisions, no guessing.
The inputs that matter
images- the batch to save; every frame gets a numbered file.output_directory- where to save. Absolute path used as-is; leave blank and connectsource_image_pathto auto-createwhite_backgroundnext to the source (that's the pair withKSLoadImageWithPath, whosesource_image_pathoutput feeds this perfectly). Leave both blank and it raises - the error tells you to do one or the other.filename_prefix- defaultwhite_background. Sanitized (illegal chars replaced), and numbering is per-prefix: existingwhite_background_00001.pngfiles make the next one..._00002, so re-running a batch appends instead of overwriting.image_format-png(lossless, keeps alpha),jpg(smaller, lossy, quality 95 with no chroma subsampling - decently clean), orwebp(smaller still, quality 95).
Outputs: saved_paths (every saved path, one per line - easy to feed into a script or an API) and saved_count. This is an output node, so it terminates a branch of your graph.
How it works
Straightforward tensor → numpy → PIL pipeline: RGBA tensors save as RGBA (so transparent backgrounds from KSApplyMaskBackground or KSProductsToBackground survive in PNG), RGB saves as RGB, and JPEG forces RGB conversion since it can't carry alpha. Files land on disk with a deterministic naming scheme that's easy to glob and easy for a human to sort.
Installation is the pack story:
cd ComfyUI/custom_nodes
git clone https://github.com/kongshan4219/ComfyUI-Kongshan-Nodes
restart ComfyUI. No models, no keys. On Linux, the directory-picker button needs zenity or kdialog (sudo dnf install -y zenity); the text path works everywhere.
Gotchas
- Numbering is per-prefix per-directory. Change the prefix and numbering restarts at 00001 - you can end up with two
*_00001.pngfiles in the same folder with different prefixes. That's by design (they're distinguishable), just know it's there. - JPEG drops alpha silently. If you save a transparent background as jpg, you get a black-background mess. Use png for anything with alpha.
- The
white_backgroundauto-folder is opinionated. The naming comes straight from the author's Alibaba-marketplace workflow (source photo →white_backgroundsubfolder for the cutout). It's a great convention - just know it's theirs, and you can override it any time by filling inoutput_directoryyourself.
The KB's image-io-metadata essay notes how central the output/ directory contract is to ComfyUI; this node is the "I know exactly where my files should go" exception to that contract, and for anyone processing dozens of listing images per session, it's quietly one of the most useful nodes in the pack.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| images | IMAGE | 要保存的图片批次。批次中每张图都会按递增编号保存。 | |
| output_directory | STRING | 输出目录。填写后直接保存到该目录;留空且连接 source_image_path 时保存到源图片旁的 white_background 子目录。 | |
| filename_prefix | STRING | white_background | 文件名前缀。节点会清理非法字符,并生成 prefix_00001.ext 形式的递增文件名。 |
| image_format | COMBO | 保存格式。png 无损适合透明/后处理;jpg 体积小但有损;webp 体积更小且适合网页。 | |
| source_image_pathopt | STRING | 源图片路径。output_directory 留空时用它推导输出目录;连接“从原始路径加载图片”的 source_image_path 最方便。 |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| saved_paths | STRING | — |
| saved_count | INT | — |