Mask Crop By Percentage
The crop node that actually finds the subject for you
- image
- mask
- crop_box
- cropped_image
- cropped_mask
- crop_box
- box_preview
Mask Crop By Percentage is the smartest node in the ComfyUI-Image-Resizing pack, and also the one that will bite you if you don't read the room. You give it an image and a mask; it finds where the mask's content actually is and crops both the image and the mask down to it. It's the difference between cropping to a frame and cropping to a subject.
That matters for a cluster of workflows. Crop-and-stitch inpainting - crop tight around the fix area, inpaint at full resolution, stitch back - lives or dies on how tight the crop is. Subject extraction wants the box around the person, not the empty half of the frame. Training crops want content-aligned boxes. This node automates the "find the box" step that you'd otherwise do by eye.
How it works
It blurs the mask, runs OpenCV contour detection, and gives you four detect modes:
- mask_area - the tight bounding box of all white pixels. The default, and usually what you want.
- min_bounding_rect - the bounding box of the largest contour.
- max_inscribed_rect - the largest rectangle that fits fully inside the mask, so it excludes concave cutouts. Nicer for faces on a body where you want the box entirely on the subject.
- crop_each_region - crops every connected white region separately and returns them as a batch, padded to a common size. This one is genuinely powerful (multiple subjects, each cropped clean) and genuinely fiddly - the batch length changes with your mask, and the padding bars can surprise you downstream.
On top of the detection, the top/bottom/left/right inputs add an inset - in percentages or pixels via use_pixel - trimming further into the detected region from each side. Note the semantics: percentages are relative to the detected box, not the whole image. top = 10 trims 10% of the detected region's height, not the frame's. That's usually what you want and it's worth knowing before you set 50 expecting a frame-wide crop. round_to_multiple snaps the final crop size up to a multiple of 8/16/32/64/128/256/512 (or None), which is your friend when the crop feeds a VAE or a Flux-style model that wants dimensions divisible by 64. There's also an optional crop_box input if you'd rather supply your own box than run detection.
The debug output that saves you
Four outputs: cropped_image, cropped_mask, crop_box (the box as ComfyUI's BOX type, wireable into other box-aware nodes), and box_preview - an image with the detected region drawn in red and the final crop in green. Glance at that preview every time before you trust the crop. Detection is opaque; the preview is the one honest window into what it decided, and it turns "why did it crop the cat's ear off?" into a two-second answer.
Gotchas
This is the fiddliest node in the pack, and that's fair - it does the most. The gaussian blur radius used for detection is hardcoded, so very soft masks can detect differently than you expect; crop_each_region returns variable-size batches padded with black; and inverting (via invert_mask) flips what counts as content. Start with mask_area, check the preview, then get fancy.
Install
Part of the standard pack install: ComfyUI Manager (search "ComfyUI-Image-Resizing") or:
cd ComfyUI/custom_nodes
git clone https://github.com/Zar4X/ComfyUI-Image-Resizing
Restart, under Image Resizing/Resizing. Dependencies are just torch/Pillow/numpy/OpenCV - no models, no downloads.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| invert_mask | BOOLEAN | false | — |
| use_pixel | BOOLEAN | false | — |
| detect | COMBO | 4 options: mask_area, min_bounding_rect, max_inscribed_rect, crop_each_region | |
| top | INT | 0-9999–9999 | — |
| bottom | INT | 0-9999–9999 | — |
| left | INT | 0-9999–9999 | — |
| right | INT | 0-9999–9999 | — |
| round_to_multiple | COMBO | 8 options: 8, 16, 32, 64, 128, 256, +2 | |
| crop_boxopt | BOX | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| cropped_image | IMAGE | — |
| cropped_mask | MASK | — |
| crop_box | BOX | — |
| box_preview | IMAGE | — |