Nodes/ComfyUI-NegiTools/Random Image Loader 🧅
ComfyUI Node

Random Image Loader 🧅

The seed is an index, not a randomizer

By natto-maki·Created 3 years ago·Updated 2 years ago· 31
Random Image Loader 🧅
    • IMAGE
    directory./input
    seed0

    You know the feeling: you have a folder of reference images and you want img2img to grab one of them at random each run, the way a batch sampler walks through prompts. ComfyUI's built-in Load Image needs you to pick a file from a dropdown. This node instead points at a directory and pulls a file for you - which is great for iterating over a folder without touching the UI every run.

    How the "random" actually works

    Here's the part the name hides: the seed input isn't a random seed at all. The node globs every PNG, JPG, and JPEG in the directory, then picks files[seed % len(files)]. It's pure index math. Same seed, same image, always, no matter how many times you run. Bump the seed by one and you step to the next file in glob order.

    That's actually a feature once you know it. Because the pick is deterministic, you can wire a Seed Generator or a counter into it and walk through your whole reference folder in a reproducible order - and come back to a specific image by restoring its seed. It only feels random, and it's fully controllable.

    Inputs and outputs

    Just two inputs: directory (default "./input", which resolves to your ComfyUI input folder) and seed. Output is a single IMAGE. Two things to know about the loading path:

    • Transparent pixels get filled with black. The loader converts to RGBA, zeroes out RGB where alpha is 0, then drops the alpha channel. PNGs with alpha come in looking flattened onto black, which matters if you're compositing later.
    • It only reads *.png, *.jpg, *.jpeg directly in the folder - no recursion, no webp. If the directory is empty or has no matching files, it throws a ValueError telling you so.

    Where you'd actually use it

    Style-reference sampling is the obvious one: drop a folder of photos in, feed one at random into a reference or img2img setup, and let the seed do the walking. It's also handy for test harnesses - feed a folder of images through the same pipeline and sweep the seed to hit every one. And because it resolves the directory relative to your ComfyUI folder, you can point it at ./input or anywhere else on disk with an absolute path.

    Installing it

    It's part of ComfyUI-NegiTools. Either install via ComfyUI Manager (search "ComfyUI-NegiTools") or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/natto-maki/ComfyUI-NegiTools
    pip install -r ComfyUI-NegiTools/requirements.txt
    

    then restart. This is a pure utility node - no API key, no model download, no runtime repo cloning. It doesn't even need the OpenAI client the pack's GPT nodes do; the heavy requirements.txt is shared across the whole pack, so you eat the install cost once regardless.

    The trap to avoid

    Because the "random" is seed % file_count, the file order is glob order, which is alphabetical-ish but not something to rely on after you add or delete files. If you add a file mid-run, every existing seed's target shifts. Don't treat the mapping as stable across folder edits - pick your seed after the folder is final, or just live with the shuffle.

    Categoryutils

    Inputs (2)

    NameTypeDefaultDescription
    directorySTRING./input
    seedINT00–18446744073709550000

    Outputs (1)

    NameTypeDescription
    IMAGEIMAGE