Nodes/KJNodes for ComfyUI/Batch Crop From Mask Advanced
ComfyUI Node Runs on cloud

Batch Crop From Mask Advanced

Crop a moving subject out of a video, jitter-free

By kijai·Created 3 years ago·Updated a day ago· 3,011
Batch Crop From Mask Advanced
  • original_images
  • masks
  • original_images
  • cropped_images
  • cropped_masks
  • combined_crop_image
  • combined_crop_masks
  • bboxes
  • combined_bounding_box
  • bbox_width
  • bbox_height
crop_size_mult1.00
bbox_smooth_alpha0.50

Say you've got a video and a mask that tracks something across it - a face, a hand, an object that moves frame to frame. You want to crop each frame down to that thing, do some work on the crops (detail, restyle, whatever), then paste them back. Doing that naively gives you a crop box that jumps around every frame because the mask wobbles, and the result looks like it was filmed during an earthquake. BatchCropFromMaskAdvanced is the node that solves the wobble.

This is the "advanced" member of the crop-from-mask family in KJNodes, and the extra machinery is all about batches and smoothing. It's the front half of the detail-a-region-in-a-video pattern, and the reason people reach for the advanced one over the basic one is the bounding-box smoothing.

How it works

It walks the batch, reads each frame's mask, and computes a bounding box around the masked region. Two controls shape what comes out. bbox_smooth_alpha smooths those boxes across frames so the crop glides instead of snapping - this is the anti-jitter knob and the whole reason this node exists. crop_size_mult scales the box up (or down) so you can grab context around the subject instead of a claustrophobic tight crop.

Then it hands you a lot of outputs, because "advanced" here means "gives you everything you might need to stitch it back." You get the crops, the cropped masks, a combined view, and - critically - the bounding boxes, so a downstream paste-back node knows exactly where each crop came from.

The inputs and outputs that matter

Inputs:

  • original_images (IMAGE) and masks (MASK) - your frames and the per-frame masks tracking the subject.
  • crop_size_mult (FLOAT, default 1.0) - how much bigger than the mask box to crop. Bump it to ~1.2–1.5 if you want margin around the subject.
  • bbox_smooth_alpha (FLOAT, default 0.5) - the jitter tamer. Higher = smoother, laggier tracking; lower = tighter tracking, more wobble. 0.5 is a sane start.

The outputs are where the power is:

  • cropped_images (IMAGE) - the crops you actually process.
  • cropped_masks (MASK) - the masks, cropped to match.
  • bboxes (BBOX) and combined_bounding_box (BBOX) - coordinates for pasting back. Keep these; they're how the round trip closes.
  • original_images (IMAGE) passthrough, plus combined_crop_image / combined_crop_masks and bbox_width / bbox_height (INT) for downstream nodes that need one consistent size.

The pattern: crop with this, run your sampler/detailer on cropped_images, then feed the result plus the bboxes to a paste-back node to composite it onto the originals.

How to install it

ComfyUI Manager: search KJNodes for ComfyUI, install, restart. Manually:

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

Restart, and it's under KJNodes/masking. No model downloads; the pack keeps dependencies minimal.

Common issues & troubleshooting

The crop still jitters. Raise bbox_smooth_alpha. If it's already high and still jumpy, the problem is upstream - a mask that flickers between frames (appearing and disappearing) will yank the box no matter how much you smooth. Fix the mask's temporal consistency first.

The subject drifts out of the crop. Smoothing lags fast motion. If your subject moves quickly, a high alpha makes the box trail behind it. Lower the alpha, or raise crop_size_mult so there's enough margin to keep the subject in frame even when tracking lags.

Paste-back lands in the wrong spot. You have to feed the same bboxes this node output to your stitching node - don't recompute them elsewhere. Mismatched boxes are the number-one reason the detailed crop ends up composited a few pixels off.

Basic would've done the job. If your subject barely moves, or you're happy cropping every frame to one shared box, the plain BatchCropFromMask is simpler. Reach for Advanced specifically when you need per-frame tracking with smoothing and the full set of paste-back coordinates.

CategoryKJNodes/masking

Inputs (4)

NameTypeDefaultDescription
original_imagesIMAGE
masksMASK
crop_size_multFLOAT1.000–10
bbox_smooth_alphaFLOAT0.500–1

Outputs (9)

NameTypeDescription
original_imagesIMAGE
cropped_imagesIMAGE
cropped_masksMASK
combined_crop_imageIMAGE
combined_crop_masksMASK
bboxesBBOX
combined_bounding_boxBBOX
bbox_widthINT
bbox_heightINT