Replace Images In Batch (Swwan)
Splice re-rendered frames back into a sequence
- original_images
- replacement_images
- original_masks
- replacement_masks
- IMAGE
- MASK
What it's for
You rendered a batch, six frames came out wrong, you fixed those six on their own, and now you want the original sequence back with the good frames swapped in. That's this node. It replaces a contiguous run of images in a batch with a shorter batch of replacements, starting at an index you choose, and it does the same for masks in parallel.
The distinction against its sibling Insert Images To Batch Indexed is worth holding onto: that one takes a list of indices and swaps arbitrary positions; this one takes a start index and does a clean range - which is what you actually want almost all the time, and it carries the masks with it.
KJNodes' ReplaceImagesInBatch, in the pack under Swwan/Advanced/Batch.
How it works
Only start_index is required (default 1, which is a small gotcha - index 1, not 0; the assumption is you kept the first frame and replaced from the second onward). Everything else is optional: original_images, replacement_images, original_masks, replacement_masks.
The range is [start_index, start_index + len(replacement)), and both ends are checked: if start_index is past the end of the batch you get ValueError: ReplaceImagesInBatch: Start index is out of range, and if the run overflows you get the same message with End index. That's refreshingly loud compared to most tensor-slicing nodes, which fail with a raw index error or, worse, silently truncate.
Two behaviours that feel thoughtful once you know them:
- Size mismatches are upscaled for you. If the replacements aren't the same H×W as the originals, they're LANCZOS-rescaled to match (masks get nearest-exact) before assignment. So you can re-render a fix at a different resolution and drop it in - but you're accepting a resample, so match sizes upstream if quality matters.
- The originals are cloned, so nothing upstream is mutated in place.
And the trap. Each side is handled all-or-nothing: if original_images and replacement_images are connected you get a spliced image batch, otherwise you get torch.zeros((1, 64, 64, 3)). The same is true of the mask pair - no masks connected means the MASK output is a 1×64×64 zeros tensor, not a passthrough and not nothing. If you wire only the image side and then feed that mask output into an inpaint or composite node, you're compositing with a 64×64 black mask and it will not tell you why nothing is happening. Either connect both pairs or ignore the mask output entirely.
Outputs are IMAGE and MASK, both matching the original batch length.
Typical wiring
Render 30 frames → spot the bad ones → select just those with a batch-range node → re-render → feed that short batch into replacement_images, the full 30 into original_images, set start_index to the first bad frame. Then save the batch, or watch it with Preview Animation to confirm the splice is invisible. Because start_index is one number, the count and position of your replacements have to be contiguous - if the bad frames aren't adjacent, either run the node twice or use Insert Images To Batch Indexed with an explicit index list.
Install
cd ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan
cd ComfyUI_Swwan
python -m pip install -r requirements.txt
Restart, refresh the browser, search Swwan. Tensor bookkeeping only - no models, no GPU requirement - and it's in Swwan/Advanced/Batch beside the rest of the batch toolkit.
Common issues
The mask output is 64×64. You didn't connect original_masks and replacement_masks. Connect both or leave that wire alone.
Start index is out of range. Default is 1 and it's the start of the range; make sure the range also fits: start_index + len(replacements) ≤ len(originals).
Only one frame got replaced. Your replacements batch is length 1, or the upstream node collapsed it. Check the count with Get Image Size & Count (Swwan).
Replaced frames look softer. They were resampled to the original's dimensions. Render fixes at the exact output size.
Image and mask batches drift apart. Both paths use the same start_index but separate lengths, so if your replacement masks are a different count from your replacement images, the two outputs stop lining up. Keep them paired.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| start_index | INT | 10–4096 | — |
| original_imagesopt | IMAGE | — | |
| replacement_imagesopt | IMAGE | — | |
| original_masksopt | MASK | — | |
| replacement_masksopt | MASK | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |
| MASK | MASK | — |