Nodes/ComfyUI-Grounding/Batch Crop and Pad From Mask
ComfyUI Node

Batch Crop and Pad From Mask

Stop Fighting Non-Divisible Crop Sizes

By PozzettiAndrea·Created 10 months ago·Updated about 18 hours ago· 47
Batch Crop and Pad From Mask
  • images
  • masks
  • cropped_images
  • cropped_masks
  • crop_info
padding_modeconstant
padding_color_r255
padding_color_g255
padding_color_b255
crop_size_mult1.0

If you've ever detected a subject, cropped it out, and tried to run a detail or inpaint pass on the crop, you know the pain this node fixes: your crop is 795x1200, the KSampler silently rounds it to 800x1200, and now your paste-back is off by pixels and the seam shows. Batch Crop and Pad From Mask crops each image to its mask's bounding box and then pads every crop to a uniform size - the largest one in the batch - so everything downstream sees consistent dimensions.

It's the batch-friendly cousin of the crop-and-paste pattern that runs through half of ComfyUI's detailer workflows (Impact Pack's detect-crop-resample-paste loop is the canonical version). The difference is this node works on whatever masks you already have, in batches, with control over how the padding fills.

How it works

From the source, it's exactly what the name promises: each image in the batch is cropped to the tight bounds of its mask, then all crops are padded to the same dimensions based on the largest crop. The crop_size_mult input (default 1.0, up to 5.0) scales the crop beyond the mask bounds - the KB's inpainting essay warns that grounding boxes are tighter than you expect (they wrap the cup, not the saucer), so a bit of extra context here is usually not optional. The KB's advice to describe the surroundings in the inpaint prompt works hand-in-glove with crop_size_mult > 1.

The inputs that matter

  • images and masks - wire in your image batch and the masks from GroundingDetector, Sam2 Segment, or GroundingMaskDetector. They must line up per-frame.
  • padding_mode - constant (fill with your RGB), edge (replicate the border pixels), or reflect (mirror the content). edge and reflect hide the pad far better than a solid color when you're doing pixel work.
  • padding_color_r/g/b - the RGB for constant padding. Defaults to white (255,255,255), which is often the wrong choice - a white pad can bleed into an inpaint; pick a color that matches the surrounding context.
  • crop_size_mult - expand the crop for context. The one knob worth tuning per workflow.

Outputs

cropped_images and cropped_masks (both uniformly sized), plus crop_info, a string of crop statistics that's surprisingly handy for debugging why a batch came out misaligned.

Installing it

Shared pack install:

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

Restart ComfyUI (or use Manager → search "Grounding"). No models involved - this node is pure tensor math, so it works immediately and uses no extra VRAM.

Where people get burned

  • Mask/image misalignment - if your masks and images don't share the same batch order, you'll silently crop the wrong regions. crop_info is your friend for spotting it.
  • Uniform size is the largest size - one huge mask makes every crop huge. If a batch has wildly different object sizes, consider splitting it so the padding doesn't balloon.
  • White-pad bleed - the constant-mode default of pure white will show up in edge pixels. For inpainting, edge mode or a context-matched color beats white nearly every time.

Worth a look even though it's the pack's least-searched node - anyone doing batch object extraction or multi-crop inpainting hits this exact problem eventually.

Categorygrounding

Inputs (7)

NameTypeDefaultDescription
imagesIMAGE
masksMASK
padding_modeCOMBOconstantconstant: use custom color | edge: replicate edges | reflect: mirror content
padding_color_roptINT2550–255Red channel for constant padding (0-255)
padding_color_goptINT2550–255Green channel for constant padding (0-255)
padding_color_boptINT2550–255Blue channel for constant padding (0-255)
crop_size_multoptFLOAT1.00.1–5Multiplier for crop size (>1.0 includes more context)

Outputs (3)

NameTypeDescription
cropped_imagesIMAGE
cropped_masksMASK
crop_infoSTRING