Image Folder Cowboy
Load a Folder of Images in the Right Order, Finally
- images
- masks
- filenames
- total_count
- file_paths
Every ComfyUI user eventually hits the ordering problem: you have img1.png … img10.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, ornone.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_byis onname- the natural sort only applies there, not todate_modifiedorsize. - "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_modeonerror, hitting the end raises by design. Switch towrap/clampif you want the run to keep going. - Batch starts repeat.
increment_by_batchoff meansimage_indexis 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.
Inputs (10)
| Name | Type | Default | Description |
|---|---|---|---|
| directory | STRING | Root directory to search for images | |
| patterns | STRING | **/*.png, **/*.jpg, **/*.jpeg, **/*.webp | Comma-separated glob patterns for matching files |
| image_index | INT | 00–999999 | Starting index (or batch index if increment_by_batch) |
| sort_by | COMBO | name | Sorting method: name uses natural sort |
| sort_order | COMBO | ascending | Sort direction |
| batch_size | INT | 11–64 | Number of images to load per execution |
| increment_by_batchopt | BOOLEAN | false | If true, index is multiplied by batch_size |
| index_modeopt | COMBO | wrap | How to handle index overflow |
| sort_subdirsopt | BOOLEAN | true | Sort subdirectories alphabetically |
| randomize_finalopt | BOOLEAN | false | Shuffle entire list after sorting |
Outputs (5)
| Name | Type | Description |
|---|---|---|
| images | IMAGE | Batch of loaded images |
| masks | MASK | Alpha channel masks (inverted, white = transparent) |
| filenames | STRING | Filenames without extension |
| total_count | INT | Total number of images found |
| file_paths | STRING | Full file paths |