Nodes/ComfyUI-Get-Random-File/Get Image File By Index ๐Ÿ–ผ๏ธ
ComfyUI Node

Get Image File By Index ๐Ÿ–ผ๏ธ

Step through a folder of images one queue at a time โ€” the non-random sibling

By ChrisColeTechยทCreated 2 years agoยทUpdated 4 days agoยท 3
Get Image File By Index ๐Ÿ–ผ๏ธ
    • image
    • filename
    • index
    • int
    • mask
    • positive_prompt
    • negative_prompt
    โ—„reset_boolfalseโ–บ
    โ—„modeincrementโ–บ
    โ—„start0โ–บ
    โ—„stop1โ–บ
    โ—„step1โ–บ
    โ—„directory_pathโ–บ

    Random is great until you want repeatable. That's the gap this node fills: instead of picking a random image like Random Image Path, it steps through the folder's images in order, one per queue run, driven by a persistent counter. Think of it as a tiny film-strip transport for your graph - you hit Queue, it advances, you get the next image.

    Why you'd want that: walking through a sequence of reference frames or animation frames in order, browsing a contact sheet one entry per generation, or building "give me image N of this folder" logic you can actually reproduce. It's an output node, so it renders a preview showing Index: x / n right on the canvas, and it hands the image off for whatever you want to do with it.

    How it works

    The counter is the whole mechanism, and it's both the feature and the gotcha. The node keeps a counter per node instance (keyed to the node's unique ID) that survives between runs. Every time the graph executes, it advances that counter according to the mode, picks files[counter] from the folder listing, and wraps around at the end. Because IS_CHANGED returns float("NaN") - the always-rerun idiom - the node fires on every queue run, which is exactly why the counter moves. If it ever didn't always re-run, stepping would silently stop.

    One thing to know before you build a sequence on it: the walk uses os.walk() and does not sort. The "index" is whatever order the filesystem's directory listing gives you - not alphabetical, not guaranteed stable. If you need a genuinely ordered sequence, name your files and test, because this is "position in the raw listing," nothing more.

    The inputs that matter

    Six inputs, but a beginner really sets two of them:

    • mode - the per-run behavior: increment and decrement step forever (wrapping at the folder's size); increment_to_stop and decrement_to_stop stop at stop instead of wrapping.
    • start / stop / step - the counter's start value, its ceiling for the _to_stop modes, and the step size.
    • reset_bool - when on, resets the counter to start on that run.
    • directory_path - the folder. Absolute path, as always with this pack.

    One quirk to know: the counter starts at start and then the mode applies on the same run, so with the defaults (start: 0, step: 1, increment) the first file you get is index 1, not 0. A small off-by-one that has confused a few people.

    Outputs

    • image (IMAGE) - the loaded image at the current index.
    • filename (STRING) - its full path.
    • index (NUMBER) and int (INT) - the same counter value in two types, so wire whichever your downstream accepts. Note it's the raw counter before wrap-around: with five files it can read 7 even though the image actually picked is file two.

    Installing it

    Same pack as the others: Manager search "ComfyUI-Get-Random-File" โ†’ install, or clone https://github.com/ChrisColeTech/ComfyUI-Get-Random-File into ComfyUI/custom_nodes and restart. No models; image loading runs on PIL and torch, which ComfyUI already ships.

    Gotchas

    • Unsorted listing โ†’ index โ‰  alphabetical order.
    • Off-by-one on the first run with defaults.
    • index/int outputs are pre-wrap values.
    • The preview is scratch - it goes to temp/, not output/.

    If your "by index" need is really "surprise me," you grabbed the wrong node - use Random Image Path instead.

    Category๐Ÿค– CCTech/Files

    Inputs (6)

    NameTypeDefaultDescription
    reset_boolBOOLEANfalseโ€”
    modeCOMBOincrement4 options: increment, decrement, increment_to_stop, decrement_to_stop
    startINT00โ€“18446744073709550000โ€”
    stopINT11โ€“18446744073709550000โ€”
    stepINT11โ€“99999โ€”
    directory_pathSTRINGโ€”

    Outputs (7)

    NameTypeDescription
    imageIMAGEโ€”
    filenameSTRINGโ€”
    indexNUMBERโ€”
    intINTโ€”
    maskMASKโ€”
    positive_promptSTRINGโ€”
    negative_promptSTRINGโ€”