Nodes/ComfyUI_Swwan/Insert Images To Batch Indexed (Swwan)
ComfyUI Node

Insert Images To Batch Indexed (Swwan)

Surgery on a batch with a text field full of numbers

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
Insert Images To Batch Indexed (Swwan)
  • original_images
  • images_to_insert
  • IMAGE
◄indexes0, 1, 2►
◄mode▾►

What it's for

You have a batch of frames and you want to swap specific ones out - or wedge new ones in between them by position. Insert Images To Batch Indexed does exactly that, and it takes the indices as a plain text field, which is either delightfully simple or infuriating, depending on how you feel about typing 3, 7, 12.

The honest use case is video repair: a clip where frames 40–45 came out mangled, re-rendered on their own, then spliced back into the original sequence so the animation stays in order. The same operation on a still batch is "replace the second and fifth images with these two retouched versions."

It's KJNodes' InsertImagesToBatchIndexed, living in the pack's Swwan/Advanced/Batch family.

How it works

Required: original_images (the batch you're editing), images_to_insert (the replacements, presumably shorter), and indexes, a multiline string that defaults to 0, 1, 2. The string is split on commas and each piece is parsed as an integer - so spaces are fine, stray characters are not.

mode picks between two quite different operations:

  • replace (the default) walks the index list and assigns images_to_insert[i] into position indexes[i]. It's a zip, which means extra indices are silently ignored if you supplied fewer insert images, and extra insert images are dropped if you supplied fewer indices. Neither is an error.
  • insert rebuilds the sequence, shifting everything right as it goes and cycling through your insert images with a modulo when there are fewer of them than indices. That cycling means five indices with two images gives you an alternating pattern rather than a crash - useful if that's what you wanted, confusing if it wasn't.

Two quirks worth knowing. Passing an empty string returns original_images untouched - a handy bypass, and also the reason a blank field never errors. And the whole thing clones the input first, so your original tensor isn't mutated in place; if something downstream looks wrong, this node is not the culprit.

Indices are zero-based, and there's no bound check in replace - an index past the end of the batch raises a raw tensor indexing error rather than a friendly message. Count your frames.

Inputs and outputs

In: original_images, images_to_insert, indexes, plus the mode enum. Out: a single IMAGE batch of the original length (replace mode) or longer (insert mode). There's no mask output, so if you're maintaining a parallel mask batch you'll need to run the same operation on it separately - or use Replace Images In Batch, which handles images and masks together for contiguous ranges.

Sizes must already match: replace assigns into an existing tensor, so the inserted images have to be the same H×W×C as the batch. Resize them upstream, or use Replace Images In Batch, which rescales mismatches for you.

Install

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

Restart ComfyUI, refresh the browser, search Swwan. Nothing here touches numpy, OpenCV or a model - it's tensor bookkeeping, and one of the quieter reasons to have this pack installed next to a Load Images From Folder node.

Common issues

Index error. An index in your string is larger than the batch. Print the batch length first - Get Image Size & Count (Swwan) gives you the count without guesswork.

Nothing changed. Either the indexes field is empty (returns input untouched by design) or you're in replace mode with more indices than insert images. Also check you didn't wire the outputs the wrong way round - it's a two-image node and both inputs are IMAGE.

Shape mismatch on the inserted images. The batch's geometry wins; insert images get no rescaling here. Add a resize step, or switch to Replace Images In Batch if the sizes are close but not equal.

Missing node after loading an old KJNodes workflow. Different ID, same data contract, no conflict aliases by design:

python scripts/migrate_workflow.py old.json --dry-run
CategorySwwan/Advanced/Batch

Inputs (4)

NameTypeDefaultDescription
original_imagesIMAGE—
images_to_insertIMAGE—
indexesSTRING0, 1, 2—
modeoptCOMBO2 options: replace, insert

Outputs (1)

NameTypeDescription
IMAGEIMAGE—