Nodes/TrentNodes/Image Folder Cowboy
ComfyUI Node

Image Folder Cowboy

Load a Folder of Images in the Right Order, Finally

By TrentHunter82·Created 9 months ago·Updated 4 days ago· 36
Image Folder Cowboy
    • images
    • masks
    • filenames
    • total_count
    • file_paths
    directory
    patterns**/*.png, **/*.jpg, **/*.jpeg, **/*.webp
    image_index0
    sort_byname
    sort_orderascending
    batch_size1
    increment_by_batchfalse
    index_modewrap
    sort_subdirstrue
    randomize_finalfalse

    Every ComfyUI user eventually hits the ordering problem: you have img1.pngimg10.png and your loader hands them back as img1, img10, img2 - because that's alphabetical, and alphabetical is wrong for numbers. Image Folder Cowboy is a directory iterator built specifically to not do that: it loads images with natural sorting, so img1 < img2 < img10 comes out in the order you actually meant.

    This is the image side of the pack's "Cowboy" family of directory iterators (Video Folder Cowboy, Text File Cowboy). They exist because per-clip, per-frame pipelines all need the same thing: a way to walk a folder in order, one batch at a time, with predictable overflow behavior when you hit the end.

    How it works

    Point directory at a folder and the node finds every file matching the patterns globs (default **/*.png, **/*.jpg, **/*.jpeg, **/*.webp). Sorting is controlled by:

    • sort_by - name (natural sort, the default and the whole point), date_modified, size, random, or none.
    • sort_order - ascending or descending.
    • sort_subdirs (default on) - sorts subdirectories alphabetically too, so recursive walks stay in order.

    Then image_index picks where to start and batch_size (1–64) how many images to load per execution. When the index runs off the end of the list, index_mode decides: wrap (loop back to the start - great for loops), clamp (stick to the last item), or error (fail loudly when you've exhausted the folder). increment_by_batch changes image_index from "starting index" to "index × batch_size," which is handy when a counter is feeding you batch numbers rather than image numbers. randomize_final shuffles after all other sorting, for training-data-style random order.

    Outputs: images (the loaded batch, as an IMAGE list), masks (alpha masks, inverted - white marks transparency), filenames (without extension), file_paths (full paths), and total_count (how many matched, so you know when the run is done).

    The workflow pattern this unlocks: pair it with the pack's Text File Cowboy keyed by the same index, and frame N of your image sequence loads with prompt N - exactly what per-frame render pipelines want. The total_count output doubles as your loop-stop signal.

    Install

    Search "Trent Nodes" in ComfyUI Manager, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/TrentHunter82/TrentNodes.git
    cd TrentNodes
    pip install -r requirements.txt
    

    Pure file loading - no models. Restart ComfyUI after installing. (The author has flagged Manager install flakiness on this pack from an early repo rename; the manual clone is the reliable route.)

    Common issues

    • Still in alphabetical order? Check sort_by is on name - the natural sort only applies there, not to date_modified or size.
    • "It skipped img5." If a file is named img5 (copy).png, natural sorting handles it, but a file with no numeric chunk sorts differently than you expect. Keep filenames regular.
    • It stopped mid-run. With index_mode on error, hitting the end raises by design. Switch to wrap/clamp if you want the run to keep going.
    • Batch starts repeat. increment_by_batch off means image_index is a starting index every run - pair it with the pack's Number Counter to step forward, or the folder never advances.

    For "load my sequence in the right order, batch by batch," this is the node. Natural sort, sane overflow modes, and a clean total_count - the things a folder loader should always have.

    CategoryTrent/Image

    Inputs (10)

    NameTypeDefaultDescription
    directorySTRINGRoot directory to search for images
    patternsSTRING**/*.png, **/*.jpg, **/*.jpeg, **/*.webpComma-separated glob patterns for matching files
    image_indexINT00–999999Starting index (or batch index if increment_by_batch)
    sort_byCOMBOnameSorting method: name uses natural sort
    sort_orderCOMBOascendingSort direction
    batch_sizeINT11–64Number of images to load per execution
    increment_by_batchoptBOOLEANfalseIf true, index is multiplied by batch_size
    index_modeoptCOMBOwrapHow to handle index overflow
    sort_subdirsoptBOOLEANtrueSort subdirectories alphabetically
    randomize_finaloptBOOLEANfalseShuffle entire list after sorting

    Outputs (5)

    NameTypeDescription
    imagesIMAGEBatch of loaded images
    masksMASKAlpha channel masks (inverted, white = transparent)
    filenamesSTRINGFilenames without extension
    total_countINTTotal number of images found
    file_pathsSTRINGFull file paths