Nodes/ComfyUI_Swwan/Shuffle Image Batch (Swwan)
ComfyUI Node

Shuffle Image Batch (Swwan)

Reproducers for random batches

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
Shuffle Image Batch (Swwan)
  • images
  • IMAGE
◄seed123►

Shuffle takes an IMAGE batch and returns the same frames in a different order - and unlike every "shuffle" in every other tool, it's reproducible. Same seed, same permutation. That's the whole reason this node beats a plain random shuffle: you can rebuild the exact ordering a week later when you decide the contact sheet was actually good.

The usual reason to have it: your downstream node cares about order and you want to break a pattern. Grid concat, video combine, and image-batch-to-list all consume frames in index order, so if you've loaded a folder of images and want a genuinely arbitrary collection rather than "the first twelve by filename sort," this is the node.

How it works

It's about as simple as a node gets, and reading the source is genuinely informative here:

torch.manual_seed(seed)
indices = torch.randperm(B)
shuffled_images = images[indices]

randperm(B) builds a permutation of the batch indices, seeded so it's deterministic; indexing with it reorders the batch. Dimensions, dtype and channel count are untouched - the risk here is only ever "which frames should be next to each other," never "did my pixels change."

That torch.manual_seed call is worth a thought: it reseeds torch's global random number generator, not a local one. In practice that's how you get reproducibility, and it's also why shuffles feel deterministic in a way that surprises people - every run with the same seed produces the identical permutation, so if you want a new order you change the seed, and if you want the same order you keep it.

Inputs and outputs

Required: images (IMAGE) and seed (INT, default 123, range 0 to 2^64−1 - a wide range, because there's no reason to be stingy). Output: a single IMAGE batch of the same length.

If you want the random order to actually vary between runs, don't hand-edit the number: wire a Seed (Swwan) output into it and set that node to randomise each time. The shuffle is only as random as the seed feeding it.

Install

Part of aining2022/ComfyUI_Swwan - one install covers the whole batch and image toolkit:

cd /path/to/ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan.git
cd ComfyUI_Swwan
python -m pip install -r requirements.txt

restart, hard-refresh, search Swwan. No models, no extra dependencies for this node - numpy/torch are already in your environment.

Where people get burned

Shuffling an already-ordered sequence you cared about. If you loaded frames from a video, shuffled them, and then encoded, you didn't make an artistic statement - you made a glitch. Keep a reverse handy: Reverse Image Batch (Swwan) flips the batch, and the pair of them is a decent cheap way to build non-sequential sequences for grids and contact sheets.

Same seed, same order - every time. People change a neighbouring parameter expecting the shuffle to move. It won't. That's the feature.

Batch, not list. This is an IMAGE tensor batch, so all frames must be the same size. A list of differently sized images won't go in; the pack's list↔batch nodes handle that conversion, and the README is blunt about the constraint: images in a tensor batch must be the same size, and empty lists can't be converted at all.

Caching. Because the node's inputs are images + seed, the engine will happily reuse a cached shuffle when neither changes. If you're building something where the order must change per queue, drive the seed from a node that changes - random each time, not a fixed number.

CategorySwwan/Advanced/Batch

Inputs (2)

NameTypeDefaultDescription
imagesIMAGE—
seedINT1230–18446744073709550000—

Outputs (1)

NameTypeDescription
IMAGEIMAGE—