Nodes/KJNodes for ComfyUI/Image Crop By Mask Batch
ComfyUI Node Runs on cloud

Image Crop By Mask Batch

Tight crops around a mask, per frame

By kijai·Created 3 years ago·Updated a day ago· 2,908
Image Crop By Mask Batch
  • image
  • masks
  • images
  • masks
width512
height512
padding0
preserve_sizefalse
bg_color0, 0, 0

This is the "zoom in on just the important part" node. Give it a batch of images and a matching batch of masks, and for each pair it crops the image tightly around wherever the mask is - a face, a detected subject, whatever region you flagged - so you can run detail work on a small, focused crop instead of the whole frame.

Why you'd want this

The classic use is a detail pass: you've got a batch of images with masks marking faces or some other small region, and you want to upscale, inpaint, or otherwise process just that region at higher effective resolution rather than wasting compute (and losing detail) processing the whole image. Cropping tight around the mask before that work, then compositing the result back afterward, is the standard "detailer" pattern - this node handles the crop half of it, per-frame, across a whole batch at once instead of one image at a time.

It pairs conceptually with the other half of that pattern: after you've processed your crops, you'd typically want to paste them back into their original positions. KJNodes ships Batch Uncrop Advanced for that - worth knowing it exists, though note it expects bounding-box (BBOX) data as input rather than the masks this node outputs, so you'd need a mask-to-bbox step somewhere in between if you're wiring the two together.

How it works

For each image/mask pair in the batch, it finds the mask's bounding region, expands it by padding, and crops the image to that area - either resized to a fixed width/height, or kept at its natural cropped size if preserve_size is on. If the padded crop extends beyond the original image's edges, bg_color fills the gap rather than the crop failing or clipping unpredictably.

The inputs and outputs that matter

  • image / masks - the batch to crop, matched pair by pair (required).
  • width / height (default 512, step 8) - the target crop size, used when preserve_size is off.
  • padding (default 0) - extra margin around the mask's bounding box before cropping.
  • preserve_size (boolean, default false) - when true, keeps each crop at its natural size (mask bounds plus padding) instead of forcing it to width×height.
  • bg_color (default "0, 0, 0") - fill color for any part of the crop that falls outside the original image.

Two outputs: images (the cropped batch) and masks (the corresponding cropped masks, so the mask stays aligned to the new crop coordinates for whatever you do next).

How to install it

  • ComfyUI Manager - search KJNodes for ComfyUI, install, restart.
  • Manual - cd ComfyUI/custom_nodes && git clone https://github.com/kijai/ComfyUI-KJNodes, then pip install -r ComfyUI-KJNodes/requirements.txt (portable: python_embeded\python.exe -m pip install -r ComfyUI\custom_nodes\ComfyUI-KJNodes\requirements.txt), then restart.

No models needed - this is geometric image processing, not ML-driven.

Common issues & troubleshooting

Crops come out at inconsistent sizes and something downstream chokes on it. That's preserve_size doing exactly what it says - each mask's bounding box is a different size, so natural crops vary. If your next node needs a fixed batch of uniform-size images (most samplers and upscalers do), turn preserve_size off and set width/height explicitly instead.

Crop includes a visible solid-color border. That's bg_color filling space outside the original image bounds - it happens when padding (or a mask sitting near the image edge) pushes the crop area past the actual picture. Reduce padding, or accept the border if you're going to composite it back with blending anyway.

Mask and image seem to drift out of alignment after cropping. Make sure you're consuming the masks output from this node for any downstream mask work, not the original pre-crop masks - the crop shifts coordinate space, and the output mask is the one that's actually aligned to the new cropped images.

CategoryKJNodes/image

Inputs (7)

NameTypeDefaultDescription
imageIMAGE
masksMASK
widthINT5120–16384
heightINT5120–16384
paddingINT00–4096
preserve_sizeBOOLEANfalse
bg_colorSTRING0, 0, 0Color as RGB values in range 0-255 or 0.0-1.0, or color name or hex code

Outputs (2)

NameTypeDescription
imagesIMAGE
masksMASK