Nodes/KJNodes for ComfyUI/Random Image From Batch
ComfyUI Node Runs on cloud

Random Image From Batch

Sampling frames without babysitting indices

By kijai·Created 3 years ago·Updated 2 days ago· 3,030
Random Image From Batch
  • input
  • output
start_index0
end_index-1
num_frames1
randomness1.00
min_distance0
max_distance0
seed0

If you've ever generated a big batch - a long AnimateDiff/Wan clip, a hundred-image seed sweep, a dataset dump - and just wanted "give me a handful of these to actually look at," this is the node. It picks a sequence of frames out of an image or mask batch, and you control exactly how random that selection is.

Why you'd want this

The obvious use is thinning down a batch for review: instead of scrolling through 300 frames, grab 12 representative ones. It's also genuinely useful for dataset curation - sampling a subset from a large generated set for captioning or LoRA training without hand-picking indices - and for pulling preview frames out of a long video render so you can sanity-check the clip before spending more compute on it.

What makes it more than a plain "grab N random items" node is the randomness knob: at 0, the picks are evenly spaced across your chosen range (a clean, deterministic sample), at 1 they're uniformly random with no repeats, and anything in between blends the two. So you can dial between "give me a representative spread" and "give me true randomness" without swapping nodes.

How it works

You give it a range within the batch (start_index to end_index), and it selects num_frames from inside that range. The output is always sorted back into batch order regardless of how it was picked - so even at maximum randomness, frame 40 still comes before frame 90 in your output, it just might not include frame 41. Negative indices count from the end, so -1 means the last frame, which is handy when you don't know or don't care how long the batch is.

Two extra controls, min_distance and max_distance, let you constrain how close together or far apart the picks can land - useful if "12 random frames" that happen to cluster all together in one corner of the batch isn't what you want. And since randomness needs a source, there's a seed for reproducibility.

The inputs and outputs that matter

  • input / output - typed COMFY_MATCHTYPE_V3, meaning this node adapts to whatever you plug in (an IMAGE batch or a MASK batch) and returns the same type back. No separate "image mode" vs "mask mode" to pick.
  • start_index / end_index - the slice of the batch to sample from (defaults 0 and -1, i.e. the whole thing).
  • num_frames - how many to pick (default 1).
  • randomness - 0 (evenly spaced) to 1 (uniform random, no replacement), default 1.
  • min_distance / max_distance - spacing constraints between picks, both default 0 (off).
  • seed - for reproducible randomness.

How to install it

  • ComfyUI Manager - search KJNodes for ComfyUI, install, restart.
  • Manual - cd ComfyUI/custom_nodes && git clone https://github.com/kijai/ComfyUI-KJNodes, then pip install -r ComfyUI-KJNodes/requirements.txt (portable: python_embeded\python.exe -m pip install -r ComfyUI\custom_nodes\ComfyUI-KJNodes\requirements.txt), then restart.

Nothing to download beyond the pack itself - this is pure indexing logic, no models involved.

Common issues & troubleshooting

num_frames is larger than the range you gave it. If your start_index/end_index slice is smaller than the count you're asking for, there simply aren't enough frames to pick without repeats - keep the requested count sane relative to the range, especially once min_distance/max_distance are also narrowing what's eligible.

Output order looks "wrong" for a random pick. It isn't a bug - the node deliberately re-sorts by batch index after selecting, so a highly random pick still comes out chronological. If you actually want the picks in selection order rather than batch order, this node won't give you that; you'd need something else downstream.

Same seed, different result after changing randomness. That's expected - randomness changes the sampling strategy itself (spaced vs. random vs. blended), so the seed producing a consistent result at one randomness value won't necessarily reproduce the same pattern at another. Lock both if you need a fully repeatable selection.

CategoryKJNodes/image

Inputs (8)

NameTypeDefaultDescription
inputCOMFY_MATCHTYPE_V3Image or mask batch to sample from.
start_indexINT0-4096–4096Inclusive start of the sampling range. Negative values count from the end.
end_indexINT-1-4096–4096Inclusive end of the sampling range. -1 means the last frame.
num_framesINT11–4096How many frames to pick from the range.
randomnessFLOAT1.000–10 = evenly spaced across the range, 1 = uniformly random without replacement, in-between = linear blend (jittered even spacing).
min_distanceINT00–4096Minimum gap (in frames) between consecutive picks. 0 = no minimum. Picks are pushed forward to satisfy this; later picks may clamp to the range end.
max_distanceINT00–4096Maximum gap (in frames) between consecutive picks. 0 = no maximum. Picks are pulled in to satisfy this, which may compress the sequence toward the start.
seedINT00–18446744073709550000Random seed for reproducible sampling. Ignored when randomness is 0.

Outputs (1)

NameTypeDescription
outputCOMFY_MATCHTYPE_V3