Nodes/ComfyUI-CustomNodeKit/图像批次替换 (Batch Replace)
ComfyUI Node

图像批次替换 (Batch Replace)

Splice generated frames into your video batch without touching the timeline

By user2318·Created 4 months ago·Updated about a month ago· 56
图像批次替换 (Batch Replace)
  • images
  • replace_images
  • image
start_index0
replace_count-1
overflow_modetake_front
underflow_modeas_is

Video pipelines in ComfyUI are just batched images, and "I need to swap out a run of frames" is one of the most common batch surgeries there is. BatchImageReplace is the node for that exact operation: it takes your main image batch, overwrites a contiguous run of frames with a different batch, and hands back the result. No timeline UI, no keyframes - just an index, a count, and some honest decisions about what to do when the numbers don't line up.

The name oversells the mystery. images is the main batch you're editing, replace_images is the batch of frames you're pasting in, and the node writes them over the main batch starting at start_index. What makes this node worth having over rolling your own is how it handles the mismatch cases, because in real workflows the two batches almost never have the exact right lengths.

The inputs that matter

  • start_index - where in the main batch the replacement begins (0-based).
  • replace_count - how many frames to replace. -1 means "everything from start_index to the end" - that's the one you'll reach for most. 0 means no replacement, straight pass-through.
  • overflow_mode - what happens when replace_images has more frames than the replacement run needs. take_front uses the first N; take_back uses the last N.
  • underflow_mode - what happens when it has fewer. as_is just replaces what it can; repeat_begin pads the front by repeating the first replacement frame; repeat_end pads the tail by repeating the last one.

Replacement frames get resized to the main batch's dimensions automatically (LANCZOS), so mismatched resolutions won't crash you - they'll just be visibly scaled.

Why you'd reach for it

The pack's WanAnimate and SCAIL-2 workflows are built around this kind of surgical edit. Concretely:

  • Hold a fixed-identity frame: replace a run of drifted frames with a frame that has the character's face right, using repeat_begin/repeat_end to pad it out.
  • Inject a keyframe: replace one position in the middle of a generated segment with a frame you actually like.
  • Trim-and-replace in loops: when a segment comes back wrong, replace just its bad range instead of re-running the whole sampler.

It's plumbing in the best sense - deterministic, no model, no magic. See the KB's node-plumbing doc for why this class of node exists at all: half of building a workflow is just learning to say "replace indices 12 through 20 with this other batch" out loud.

Install

Ships in user2318/ComfyUI-CustomNodeKit (one of the pack's "merged" batch nodes, grouped with its sibling BatchFrameReplicate). ComfyUI Manager: search "CustomNodeKit", or:

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

then restart. Pure torch ops, no models to download.

The gotcha

replace_count = -1 means "to the end", and the node silently limits the actual replacement to the main batch's remaining length. If start_index is past the end of the batch, it warns in the console and returns the batch unchanged - no error, no output difference. That silent fallback is the thing that'll confuse you in a loop: the batch shrank somewhere upstream, start_index went out of range, and the node quietly stopped doing anything. If replacements mysteriously stop applying, check start_index against your actual batch length first. And remember the ordering isn't validated - the node assumes your frames are already in the order you want to paste, garbage in, garbage out.

Categoryimage

Inputs (6)

NameTypeDefaultDescription
start_indexINT00–4294967295从主批次的哪个位置开始替换(0-based)。Starting position for replacement (0-based).
replace_countINT-1-1–4294967295要替换多少张图像。 -1 = 全部替换(从start_index到末尾),0 = 不替换。实际可替换数量受主批次剩余长度限制。Number of images to replace. -1=replace all (from start_index to end), 0=no replacement. Actual count limited by remaining batch length.
overflow_modeCOMBOtake_front替换图像批次数量多于需替换张数时的处理方式。take_front=从前面截取所需张数;take_back=从后面截取所需张数。When replace_images has MORE frames than needed: take_front=use first N; take_back=use last N.
underflow_modeCOMBOas_is替换图像批次数量少于需替换张数时的处理方式。as_is=按实际张数替换(不扩展);repeat_begin=用第1张重复填充前面(如[A,B]→[A,A,A,B]);repeat_end=用最后1张重复填充后面(如[A,B]→[A,B,B,B])。When replace_images has FEWER frames than needed: as_is=use actual count; repeat_begin=fill front with first image; repeat_end=fill end with last image.
imagesoptIMAGE主图像批次 [B, H, W, C]。将要在此批次中替换指定位置的图像。Main image batch [B, H, W, C]. Images will be replaced at specified positions.
replace_imagesoptIMAGE替换用图像批次 [B, H, W, C]。替换来源图像,未连接时直接透传主批次。Replacement image batch [B, H, W, C]. When disconnected, passes through original images unchanged.

Outputs (1)

NameTypeDescription
imageIMAGE