Nodes/KJNodes for ComfyUI/Replace Images In Batch
ComfyUI Node Runs on cloud

Replace Images In Batch

Splice regenerated frames back into a batch

By kijai·Created 3 years ago·Updated a day ago· 3,011
Replace Images In Batch
  • original_images
  • replacement_images
  • original_masks
  • replacement_masks
  • IMAGE
  • MASK
start_index1

Here's the situation this node is built for: you've got a batch of images - say the 81 frames of a video - and a handful of them came out wrong. You fix just those frames, and now you need to drop the fixed ones back into the full batch at the right spot without re-rendering everything. ReplaceImagesInBatch does exactly that. Feed it the original batch, the replacements, and the index to start at, and it hands back the spliced result.

It's one of the many batch-surgery utilities in Kijai's KJNodes. The pack is a grab-bag of quality-of-life nodes that keep showing up in downloaded workflows, and the batch/latent manipulation nodes are some of the most useful once you're doing video or multi-image work - the stuff core ComfyUI doesn't quite give you.

How it works

A batch in ComfyUI is just a stack of images with a shared index. This node overwrites a contiguous run of that stack, starting at start_index, with your replacement images. As the node's own description puts it: it "replaces the images in a batch, starting from the specified start index, with the replacement images." Everything before the index and everything after the replaced run stays untouched. It handles masks in parallel, so you can splice a matching run of masks at the same time.

The inputs and outputs that matter

  • start_index (INT, default 1) - where the replacement begins. This is the knob you'll actually set. Index 0 is the first frame.
  • original_images (IMAGE) - the batch you're splicing into.
  • replacement_images (IMAGE) - the frames going in. However many you provide is how many get replaced, counting up from start_index.
  • original_masks / replacement_masks (MASK) - the same operation for a mask batch, if you're carrying masks alongside your images.

Outputs are IMAGE (the spliced batch) and MASK (the spliced mask batch). Wire the IMAGE onward to your save/encode; the MASK is there for when you're keeping image and mask batches in lockstep.

How to install it

ComfyUI Manager: search KJNodes for ComfyUI, install, restart. Or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/kijai/ComfyUI-KJNodes
pip install -r ComfyUI-KJNodes/requirements.txt

then restart. Portable build: run the pip line through python_embeded\python.exe. No models or heavy deps.

Common issues & troubleshooting

Dimensions have to match. The replacement frames need the same width and height as the batch you're splicing into. If you regenerated at a different resolution, resize them back first or you'll get a shape error (or a garbled composite).

Your replacements ran off the end. If you start at index 70 of an 81-frame batch and hand it 20 replacement frames, you're asking to replace positions that don't exist. Keep start_index + number_of_replacements within the batch length.

Off-by-one on the start index. Indices are 0-based - frame one is index 0. The default of 1 replaces starting from the second frame, which trips people up. If you meant to replace the very first frame, set it to 0.

Masks and images drifted apart. If you're splicing both, replace the same run in both - same start_index, same count - or your masks stop lining up with your frames. This is the quiet bug that makes a later inpaint or composite look off for no obvious reason.

CategoryKJNodes/image

Inputs (5)

NameTypeDefaultDescription
start_indexINT10–4096
original_imagesoptIMAGE
replacement_imagesoptIMAGE
original_masksoptMASK
replacement_masksoptMASK

Outputs (2)

NameTypeDescription
IMAGEIMAGE
MASKMASK