Nodes/Kongshan Nodes/保存图片到自定义目录
ComfyUI Node

保存图片到自定义目录

Save a Batch Straight to Any Folder You Choose (or Next to the Source)

By kongshan4219·Created 3 months ago·Updated 3 months ago· 0
保存图片到自定义目录
  • images
  • saved_paths
  • saved_count
output_directory
filename_prefixwhite_background
image_format
source_image_path

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 connect source_image_path to auto-create white_background next to the source (that's the pair with KSLoadImageWithPath, whose source_image_path output feeds this perfectly). Leave both blank and it raises - the error tells you to do one or the other.
  • filename_prefix - default white_background. Sanitized (illegal chars replaced), and numbering is per-prefix: existing white_background_00001.png files 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), or webp (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.png files 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_background auto-folder is opinionated. The naming comes straight from the author's Alibaba-marketplace workflow (source photo → white_background subfolder for the cutout). It's a great convention - just know it's theirs, and you can override it any time by filling in output_directory yourself.

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.

CategoryKongshan/Local

Inputs (5)

NameTypeDefaultDescription
imagesIMAGE要保存的图片批次。批次中每张图都会按递增编号保存。
output_directorySTRING输出目录。填写后直接保存到该目录;留空且连接 source_image_path 时保存到源图片旁的 white_background 子目录。
filename_prefixSTRINGwhite_background文件名前缀。节点会清理非法字符,并生成 prefix_00001.ext 形式的递增文件名。
image_formatCOMBO保存格式。png 无损适合透明/后处理;jpg 体积小但有损;webp 体积更小且适合网页。
source_image_pathoptSTRING源图片路径。output_directory 留空时用它推导输出目录;连接“从原始路径加载图片”的 source_image_path 最方便。

Outputs (2)

NameTypeDescription
saved_pathsSTRING
saved_countINT