Nodes/😸 YFG Comical Nodes/🐯 YFG Random Image From Directory
ComfyUI Node

🐯 YFG Random Image From Directory

The folder-aware random image picker with a built-in file browser

By gonzaluΒ·Created 2 years agoΒ·Updated 8 days agoΒ· 29
🐯 YFG Random Image From Directory
    • image
    • path_current
    • index_current
    • filename_current
    • width
    • height
    • sha256
    • total_count
    • path_previous
    • index_previous
    β—„image_directoryβ€”β–Ί
    β—„include_subdirstrueβ–Ί
    β—„selection_moderandomβ–Ί
    β—„show_previewfalseβ–Ί
    β—„index0β–Ί
    β—„filename_queryβ€”β–Ί
    β—„random_sourceautoβ–Ί
    β—„ensure_uniquefalseβ–Ί
    β—„unique_scopeβ–Ύβ–Ί
    β—„history_size512β–Ί
    β—„time_window_sec0β–Ί
    β—„retry_limit16β–Ί

    "Load a random image from a folder" sounds like a five-minute node, and most implementations are - a dropdown, a glob, done. This one from the YFG Comical pack (gonzalu/ComfyUI_YFG_Comical) is the over-engineered version, and mostly in a good way: a server-side folder browser so you don't hand-type paths, four selection modes, optional true randomness via Random.org, session dedup so it doesn't show you the same image twice, and a pile of metadata outputs including a SHA-256 hash. If you're rotating reference images, auditing a dataset, or building an image-queue workflow, it's the rare "random image" node that doesn't make you compromise.

    Selection modes

    selection_mode is the heart of it:

    • random - pick a random file. The randomness source is random_source: auto uses Random.org if a key is configured (true randomness), otherwise falls back to the local PRNG; local forces local; random_org forces the API (and requires the key).
    • by_index - pick the file at a specific index. Out-of-bounds values are clamped, not wrapped, which is the sane behavior.
    • by_filename - first file matching an exact or substring name.
    • by_query - glob-style wildcards (*.png, cat*), then random among the matches.

    include_subdirs (default on) recurses, and files are naturally sorted, so index is deterministic and stable across runs.

    The extras that make it useful

    • Directory browser: a Browse for Directory button opens a navigable server-side folder picker (the node registers /yfg/dir_browse API routes), and Recent Directories shows your MRU list. Used dirs auto-save to yfg_dir_history.json. This kills the "typo a path, get nothing" class of bugs completely.
    • No repeats: ensure_unique (off by default - turn it on) remembers recent picks per-directory or globally (unique_scope), bounded by history_size (default 512) and time_window_sec (0 = forever). Session-lifetime only.
    • Current vs previous: outputs carry both path_current/index_current and path_previous/index_previous, so you can build "show the last two picks" workflows or a compare grid without extra plumbing.
    • Preview: show_preview renders a thumbnail on the node (and publishes preview metadata compatible with UI-driven nodes).

    Outputs

    Ten of them, and they're a gift for automation: image, path_current, index_current, filename_current, width, height, sha256 (handy for dedup/auditing), total_count, path_previous, index_previous. The inline value display shows the key ones on the output sockets after each run.

    Installing and setup

    Pack install: ComfyUI Manager β†’ search "YFG Comical", or

    cd ComfyUI/custom_nodes
    git clone https://github.com/gonzalu/ComfyUI_YFG_Comical
    

    Restart ComfyUI. Optional true-randomness setup: put a key in random_org_api_key.json next to RandomOrgV2.py (or RANDOM_ORG_API_KEY env var) - same key the pack's Random Number nodes use. Note the browser routes print a warning to the console if they can't register; that's cosmetic, the node still works.

    The honest take

    This is the most feature-dense node in the pack, and the tradeoff shows: the input list is intimidating for what is conceptually a file picker. The three settings that matter on day one are image_directory, selection_mode, and ensure_unique; the rest are there when you need them. The by_query mode plus sha256 output makes it a genuinely nice little dataset tool. If you just want "one random image," the built-in approach works - but if you want it done right, this is the one.

    Category🐯 YFG/πŸ–ΌοΈ Loaders

    Inputs (12)

    NameTypeDefaultDescription
    image_directorySTRINGDirectory containing image files to pick from.
    include_subdirsBOOLEANtrueIf true, search subfolders recursively.
    selection_modeCOMBOrandomHow to pick the image: random, by_index, by_filename, or by_query (wildcard).
    show_previewBOOLEANfalseIf enabled, show a preview thumbnail (and publish UI preview metadata for downstream nodes).
    indexINT00–18446744073709550000Used only for by_index. Out-of-bounds is clamped: <0β†’0, >=countβ†’last.
    filename_querySTRINGUsed by by_filename/by_query. by_query supports * and ? wildcards.
    random_sourceCOMBOautoauto uses random.org if API key exists; otherwise uses local random.
    ensure_uniqueBOOLEANfalseIf true, avoid recently-used images (based on history_size/time_window_sec).
    unique_scopeCOMBOdirectory: uniqueness tracked per-directory. global: shared across all directories.
    history_sizeINT5121–100000How many recent selections to remember for uniqueness checks.
    time_window_secINT00–604800If >0, forget uniqueness history entries older than this many seconds.
    retry_limitINT161–999Maximum attempts to find a unique candidate before falling back.

    Outputs (10)

    NameTypeDescription
    imageIMAGEThe selected image as an IMAGE tensor.
    path_currentSTRINGFull path to the currently selected image file.
    index_currentINT0-based index of the selected image within the (sorted) file list.
    filename_currentSTRINGFilename of the selected image (basename only).
    widthINTImage width (pixels).
    heightINTImage height (pixels).
    sha256STRINGSHA-256 hash of the file contents (useful for de-duping / auditing).
    total_countINTTotal number of images discovered in the directory (and subdirs if enabled).
    path_previousSTRINGFull path to the previously selected image in this ComfyUI session.
    index_previousINT0-based index of the previously selected image in this ComfyUI session.