Nodes/ComfyUI_Swwan/Load Images From Folder (Swwan)
ComfyUI Node

Load Images From Folder (Swwan)

Point it at a directory and get a batch back

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
Load Images From Folder (Swwan)
    • image
    • mask
    • count
    • image_path
    ◄folder►
    ◄width1024►
    ◄height1024►
    ◄keep_aspect_ratio▾►
    ◄image_load_cap0►
    ◄start_index0►
    ◄include_subfoldersfalse►

    What it's for

    ComfyUI loads one image at a time. If you want to process a folder - a LoRA dataset, a batch of frames you exported, two hundred renders you want to re-pass at higher denoise - you either click Load Image a hundred times or you use a node like this one.

    Load Images From Folder (KJNodes' LoadImagesFromFolderKJ) takes a folder path as a string, globs the images inside it, resizes them all to a common size, and returns them as one stacked IMAGE batch plus a matching mask batch and a count. It's the loader side of "dataset prep in ComfyUI without leaving ComfyUI."

    How it works

    The folder is a plain string input, not a dropdown, so there's no enumeration cost at startup - but it also means typos fail at execution. It reads .jpg, .jpeg, .png, .webp and .tga, sorted by name, and does not recurse unless you switch include_subfolders on. start_index slices the sorted list before loading, and image_load_cap cuts it off after N images; combined they're your page-through-a-huge-folder mechanism.

    Sizing is where most of the confusion lives. width and height are two integers plus a keep_aspect_ratio mode:

    • crop scales to cover the target and centre-crops the overhang.
    • pad fits inside and pads the remainder.
    • stretch just squashes it to the box.

    Set both width and height to -1 and it uses each file's own dimensions - but then everything must coincidentally match, because the loader stacks them into a single batch at the end. Mixed sizes plus -1 is a shape error waiting to happen; pick a real target instead.

    Alpha becomes the mask: 1 - alpha where a file has an alpha channel, zeros where it doesn't. That's the ComfyUI convention, and it means your transparent PNGs give you usable masks for free.

    Two details that make it nicer than it looks. The node overrides IS_CHANGED with a folder hash - path, file count, and every file's mtime - so adding or editing a file in the folder re-triggers the node instead of serving a cached batch forever. That's the no-surprises behaviour an always-dirty node would give you, without poisoning the cache for everything downstream. And the count output means you can log or branch on "how many images did I actually load" without a separate node.

    Outputs: image, mask, count and image_path. Read that last one carefully - when it loads more than one file it returns a list of paths, even though the socket is typed STRING. A node expecting a single string will do something unhelpful with that.

    Install

    cd ComfyUI/custom_nodes
    git clone https://github.com/aining2022/ComfyUI_Swwan
    cd ComfyUI_Swwan
    python -m pip install -r requirements.txt
    

    Restart, hard-refresh the browser, search Swwan. The pack's requirements (Pillow, numpy, OpenCV, scipy, scikit-image) cover it; there's no model and no GPU path here.

    Common issues

    FileNotFoundError: Folder '...' cannot be found. Give it an absolute path. If you keep images inside ComfyUI, note that relative paths resolve against the base directory your install was started from, not against input/ - which is the opposite of what Load Image does.

    No files in directory '...'. Either the folder is empty or everything in it is a format it doesn't read (.bmp, .avif and .gif are not in the list). Convert first.

    Everything OOMs or takes forever. You're loading 400 images into one batch at full resolution. Use image_load_cap and start_index to chunk it, or drop the resolution.

    Unstable results after editing files in the folder. The hash covers mtimes; if your sync tool preserves timestamps, the node may not notice. Bump start_index or touch the files.

    The path output isn't a string. It's a list when multiple files load. Downstream nodes that expect one path will misbehave - take the count and do your own indexing.

    CategorySwwan/Advanced/IO

    Inputs (7)

    NameTypeDefaultDescription
    folderSTRING—
    widthINT1024—
    heightINT1024—
    keep_aspect_ratioCOMBO3 options: crop, pad, stretch
    image_load_capoptINT0—
    start_indexoptINT0—
    include_subfoldersoptBOOLEANfalse—

    Outputs (4)

    NameTypeDescription
    imageIMAGE—
    maskMASK—
    countINT—
    image_pathSTRING—