Random Images From Batch
Grab a random handful from a batch — with a couple of guest-list exceptions
- images
- IMAGE
Random Images From Batch does what its name promises - picks a random subset from a batch of images - but with a twist that makes it genuinely useful rather than a toy: you get to say which ones must be included and which ones must not. Random selection plus a guest list, essentially. You decide the count, mark a few must-keeps, blacklist a few, and the node fills the rest by chance.
The use case that justifies the extra controls is quality control on a large batch. You generated 50 images and you want to eyeball a random sample of 5 - that's just the count and the seed. But you also suspect a couple of specific frames are broken, so you drop them in exclude_list and they can't pollute your review. Or you're building a training sample and you know two images are your prize shots - mandatory_list pins them in while the rest get randomly drawn. It's random selection with guardrails, and the guardrails are the point.
How it works
It reads your comma-separated lists (1-based indices), clamps everything to the batch size, dedupes, and then: mandatory picks go in first, the excluded ones are removed from the pool, and the remaining slots are filled by a seeded shuffle. Same seed, same selection - the random part is reproducible, which is what you want when the selection turns out to be the one that matters. The output preserves the original image order of the chosen indices rather than the shuffle order. If everything ends up excluded, you get an empty batch tensor (shape 0×H×W×C) rather than a crash - which downstream nodes may or may not handle gracefully.
Inputs and outputs
- images - the batch to draw from
- count - INT, default 1, minimum 1. How many to select
- mandatory_list - comma-separated 1-based indices, always included
- exclude_list - comma-separated 1-based indices, never included
- seed - INT, default 0. Determinism for the random fill
Output is a single IMAGE (the selected subset, still a batched tensor).
Installing it
Standard XJNodes install. ComfyUI Manager → search "ComfyUI-XJNodes" → install → restart. Or:
cd ComfyUI/custom_nodes
git clone https://github.com/alexjx/ComfyUI-XJNodes
# restart ComfyUI
No extra dependencies - pure torch indexing. Category: XJNodes/image.
The fiddly bits worth knowing
Remember the lists are 1-based, because everything in the ComfyUI UI is, and it's the most common off-by-one here. If you can't tell whether you're about to write "3" for the third image or the fourth, assume the UI convention. Also, if mandatory_list has more entries than count, the extras get truncated - the node takes the first N of your mandatory list, so list them in priority order if that matters. And if count exceeds the available pool, it just returns what it can, which means you can silently get fewer images than you asked for - worth checking if the downstream pipeline needs an exact count. For the "grab exactly one specific frame" version of this, the pack's One Image From Batch is the deterministic sibling.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| images | IMAGE | — | |
| count | INT | 1 | — |
| mandatory_listopt | STRING | — | |
| exclude_listopt | STRING | — | |
| seedopt | INT | 00–18446744073709550000 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |