Nodes/cspnodes/Image Dir Iterator
ComfyUI Node

Image Dir Iterator

Step through a folder of images with real sort and batch control

By cerspense·Created 2 years ago·Updated 7 months ago· 36
Image Dir Iterator
    • IMAGE
    • MASK
    • STRING
    • INT
    directory_path
    glob_patterns**/*.png, **/*.jpg
    image_index0
    sort_by
    sort_order
    batch_size1
    increment_by_batchfalse
    randomize_final_listfalse

    This is the node to reach for when "Load Image" isn't enough - when you want to walk through a whole folder (or several, via multiple glob patterns) with actual control over ordering, batching, and where you are in the list, instead of hardcoding one file path at a time.

    How it works

    Give it a directory and one or more comma-separated glob patterns (the default is **/*.png, **/*.jpg, and ** means it recurses into subfolders). It matches files across all your patterns, filters down to real image extensions, then groups the results by subdirectory and sorts within each subdirectory group before concatenating the groups back together in the order they were first encountered. Then, if you've turned on randomize_final_list, it shuffles the whole combined list on top of that.

    The index wraps around automatically using modulo arithmetic - run past the end of your file list and it just loops back to the start instead of erroring, which makes it genuinely usable as the index-driver in a repeating batch job.

    The inputs and outputs that matter

    • directory_path / glob_patterns - where to look and what to match. Multiple patterns are comma-separated.
    • image_index - which position to start from (with wraparound, as above).
    • sort_by - date_modified, name, size, or random.
    • batch_size (1–64) - how many images to grab per call.
    • increment_by_batch - off by default, meaning image_index moves through the list one image at a time even while returning batch_size images per call. Turn it on and the index instead advances by a full batch_size each call, so you get non-overlapping chunks.
    • randomize_final_list - shuffles everything after the sort step, not instead of it.

    Outputs, four of them: IMAGE (the batch), MASK (built from each image's alpha channel if it has one, otherwise empty), STRING (the matched filenames, extension stripped), and INT (the total number of files found by your patterns - not the batch count, the full match count, useful for wiring into a loop-bound check or pairing with IncrementEveryN, same pack, to know how many iterations a full pass actually needs).

    Where people get tripped up

    The one worth knowing before it bites you: sort_by: "name" is not a plain alphabetical sort. It pulls out only the digit characters from each filename and sorts by that number. This is actually the right behavior for the common case - img5.png correctly sorts before img10.png, which a naive string sort would get backwards - but it means any filename with no digits in it at all sorts as if its "name" were 0. Mix a file like cover.png into a folder full of frame001.png through frame100.png sorted by name, and cover.png jumps straight to the front regardless of what you'd expect from its actual filename.

    Also worth noting: the default glob pattern only matches .png and .jpg. The node itself will happily accept .jpeg, .bmp, .gif, and .webp too, but only if your glob_patterns actually includes a pattern that matches them - if your folder is full of WebP files and you're getting a "no files found" error, that's usually why.

    Installing it

    Search cspnodes in ComfyUI Manager, or clone it directly:

    cd ComfyUI/custom_nodes
    git clone https://github.com/cerspense/ComfyUI_cspnodes
    

    Restart after. The pack imports diffusers and pymediainfo unconditionally at the top of its single Python file, for other nodes in the pack - if cspnodes fails to show up at all rather than just this node misbehaving, check your startup log for one of those two failing to import.

    Categorycspnodes

    Inputs (8)

    NameTypeDefaultDescription
    directory_pathSTRING
    glob_patternsSTRING**/*.png, **/*.jpg
    image_indexINT0
    sort_byCOMBO4 options: date_modified, name, size, random
    sort_orderCOMBO2 options: ascending, descending
    batch_sizeINT11–64
    increment_by_batchBOOLEANfalse
    randomize_final_listBOOLEANfalse

    Outputs (4)

    NameTypeDescription
    IMAGEIMAGE
    MASKMASK
    STRINGSTRING
    INTINT