Nodes/ComfyUI-Get-Random-File/Random Image Path 🎲
ComfyUI Node

Random Image Path 🎲

A random image from a folder on every run β€” img2img roulette, minus the setup

By ChrisColeTechΒ·Created 2 years agoΒ·Updated 4 days agoΒ· 3
Random Image Path 🎲
    • image
    • filename
    • mask
    • positive_prompt
    • negative_prompt
    β—„directory_pathβ–Ί
    β—„randomizetrueβ–Ί

    The setup: you've got a folder of reference images - reference shots, style samples, your last fifty generations - and you want each queue run to grab a different one and feed it into the graph. That's exactly what Random Image Path does: pick a random image from a directory, load it, and hand you the pixels as a ready-to-use IMAGE plus the filename as a STRING.

    Where people actually reach for it: img2img roulette (every run starts from a new random base), shuffling a reference into an IPAdapter or ControlNet branch, or batch-sampling through a folder of style sheets without hand-wiring each one. Because the image comes out already normalized with a batch dimension - the same shape a LoadImage produces - it drops straight into a VAE Encode, an img2img sampler, or a reference-processing chain. No conversion step.

    How it works

    Recursive os.walk() again, this time filtered to image extensions: .jpg, .jpeg, .png, .bmp, .tiff, .webp. One is picked with random.choice(), then loaded with PIL. The loader is the polite kind: it runs exif_transpose so phone photos aren't sideways, converts to RGB, and normalizes to 0–1 floats. It also copies the pick into ComfyUI's temp/ folder so the node shows a live preview of what it chose right on the canvas. Like its sibling, it returns float("NaN") from IS_CHANGED - the always-rerun idiom - and that's how you get a new random image every Queue rather than every time you look at the workflow. Same caveat applies: this node makes everything downstream re-execute each run. That's the point; just don't be surprised when the graph after it never skips.

    The inputs and outputs that matter

    • directory_path - absolute path to the folder. That's the whole input surface.
    • image (IMAGE) - the loaded, normalized image, ready for anything expecting a LoadImage-style tensor.
    • filename (STRING) - the full path of the file that was picked. Handy if you want to log the source, save the filename next to the output, or feed it to a text-side node.

    Installing it

    Manager search "ComfyUI-Get-Random-File" β†’ install, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/ChrisColeTech/ComfyUI-Get-Random-File
    

    then restart. No models to download. The pack's pinned dependencies (opencv-python, imageio-ffmpeg) exist for its video nodes; image picking here runs on PIL and torch, which ComfyUI already ships.

    Gotchas

    • The preview is scratch. It lands in temp/, not output/ - it's for looking at, not for keeping. Don't go hunting for it in your output folder.
    • Absolute path or nothing. An empty or invalid directory_path throws NotADirectoryError. There's no fallback to ComfyUI's input/ convention; this node reads wherever you point it.
    • Subfolders count. Recursion means nested images are all in the pool.
    • Always re-runs. New pick per queue; the cost is the downstream cache.

    If you want a specific image rather than a random one - stepping through the folder in order, say - the sibling Get Image File By Index node from the same pack is the tool for that.

    CategoryπŸ€– CCTech/Files

    Inputs (2)

    NameTypeDefaultDescription
    directory_pathSTRINGβ€”
    randomizeoptBOOLEANtrueOn (default) = pick a new random file every run. Off = keep returning the currently selected file (per node; re-picks only if that file no longer exists).

    Outputs (5)

    NameTypeDescription
    imageIMAGEβ€”
    filenameSTRINGβ€”
    maskMASKβ€”
    positive_promptSTRINGβ€”
    negative_promptSTRINGβ€”