π― YFG Random Image From Directory
The folder-aware random image picker with a built-in file browser
- image
- path_current
- index_current
- filename_current
- width
- height
- sha256
- total_count
- path_previous
- index_previous
"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 israndom_source:autouses Random.org if a key is configured (true randomness), otherwise falls back to the local PRNG;localforces local;random_orgforces the API (and requires the key).by_index- pick the file at a specificindex. 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_browseAPI routes), and Recent Directories shows your MRU list. Used dirs auto-save toyfg_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 byhistory_size(default 512) andtime_window_sec(0 = forever). Session-lifetime only. - Current vs previous: outputs carry both
path_current/index_currentandpath_previous/index_previous, so you can build "show the last two picks" workflows or a compare grid without extra plumbing. - Preview:
show_previewrenders 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.
Inputs (12)
| Name | Type | Default | Description |
|---|---|---|---|
| image_directory | STRING | Directory containing image files to pick from. | |
| include_subdirs | BOOLEAN | true | If true, search subfolders recursively. |
| selection_mode | COMBO | random | How to pick the image: random, by_index, by_filename, or by_query (wildcard). |
| show_preview | BOOLEAN | false | If enabled, show a preview thumbnail (and publish UI preview metadata for downstream nodes). |
| index | INT | 00β18446744073709550000 | Used only for by_index. Out-of-bounds is clamped: <0β0, >=countβlast. |
| filename_query | STRING | Used by by_filename/by_query. by_query supports * and ? wildcards. | |
| random_source | COMBO | auto | auto uses random.org if API key exists; otherwise uses local random. |
| ensure_unique | BOOLEAN | false | If true, avoid recently-used images (based on history_size/time_window_sec). |
| unique_scope | COMBO | directory: uniqueness tracked per-directory. global: shared across all directories. | |
| history_size | INT | 5121β100000 | How many recent selections to remember for uniqueness checks. |
| time_window_sec | INT | 00β604800 | If >0, forget uniqueness history entries older than this many seconds. |
| retry_limit | INT | 161β999 | Maximum attempts to find a unique candidate before falling back. |
Outputs (10)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | The selected image as an IMAGE tensor. |
| path_current | STRING | Full path to the currently selected image file. |
| index_current | INT | 0-based index of the selected image within the (sorted) file list. |
| filename_current | STRING | Filename of the selected image (basename only). |
| width | INT | Image width (pixels). |
| height | INT | Image height (pixels). |
| sha256 | STRING | SHA-256 hash of the file contents (useful for de-duping / auditing). |
| total_count | INT | Total number of images discovered in the directory (and subdirs if enabled). |
| path_previous | STRING | Full path to the previously selected image in this ComfyUI session. |
| index_previous | INT | 0-based index of the previously selected image in this ComfyUI session. |