Nodes/ComfyUI-Conditioning-Rebalance/Mask Aspect Ratio BBox
ComfyUI Node

Mask Aspect Ratio BBox

Fit the crop window to the thing you actually masked

By nova452·Created 3 months ago·Updated 27 days ago· 509
Mask Aspect Ratio BBox
  • mask
  • MASK
  • chosen_resolution
aspect_ratios1000x1000 768x1344 1344x768 832x1216 1216x832
crop_positioncenter
enforce_min_pixelsfalse

Cropping around a mask is the step everyone hand-rolls and nobody gets right the first time. You mask a face, crop to the mask bounding box, and the crop is some random aspect ratio the model never trained on - or it's a tiny sliver with no context around the subject. MaskAspectRatioBBox from the Rebalance Pack is the version that thinks in both dimensions at once: it takes a mask, finds the bounding box of what you actually marked, then expands (or tightens) that box to the closest aspect ratio from your list, and returns the adjusted box as a mask.

It's the geometry partner to the pack's ImageAspectRatioCrop - same ratio list, same crop_position logic, but driven by a mask instead of the whole image. If you've ever built a detailer-style crop-and-edit pipeline (crop the face, run it at native resolution, paste it back), this is the node that makes the crop window behave.

How it works

For each frame it finds the non-zero pixels of the mask, computes the bounding box, and compares the box's aspect ratio against every entry in your multiline aspect_ratios list, ordered by closeness. Then comes the genuinely useful part: for each candidate ratio it works out both a crop rect (inside the box, ratio-matched) and a pad rect (outside the box, ratio-matched) and picks whichever loses less of the masked region. The box gets expanded when that gives a better ratio - so a face mask that's slightly off-center still ends up with a well-framed crop instead of a corner-clipped one.

enforce_min_pixels makes sure the chosen rect is at least the target resolution on each side, which is how you guarantee the crop lands at a resolution the model actually wants to work at. The crop_position dropdown (center/top/bottom/left/right) controls where the rect anchors relative to the box when it can't fit the image exactly.

Outputs are MASK - a 1.0-filled rectangle at the chosen crop window, same size as the input mask - and chosen_resolution, a STRING reporting the WxH ratio that won. Feed the rect mask into an image crop or a detailer, and keep the string if you want to know what the model is about to see.

The inputs that matter

  • mask - what you're cropping around.
  • aspect_ratios - the multiline list. Defaults to the same 1000x1000/768x1344/etc. buckets as its sibling.
  • crop_position - anchoring; center is fine most of the time.
  • enforce_min_pixels - on when you need the crop big enough to be useful.

Install

Part of the Rebalance Pack. ComfyUI Manager: search "ComfyUI-Conditioning-Rebalance" (or "Rebalance Pack"). Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/nova452/ComfyUI-ConditioningKrea2Rebalance

Restart afterwards. Pure torch - no models, no extra deps.

Troubleshooting

If the mask is empty the node returns it unchanged with a 0x0 resolution string - that's the "nothing selected" signal, and downstream crop logic should treat it as a no-op. The enforce_min_pixels mode will happily build a rect bigger than the image; when that happens the node just can't place it and skips that candidate, so if you're getting 0x0 back with a real mask, check that the enforced size fits the frame. And remember the output is the window, not the content - you still need to crop the image to match, which is where UncropImage from the same pack comes back in to paste the results where they belong.

CategoryRebalance-Pack/mask

Inputs (4)

NameTypeDefaultDescription
maskMASK
aspect_ratiosSTRING1000x1000 768x1344 1344x768 832x1216 1216x832
crop_positionCOMBOcenter5 options: center, top, bottom, left, right
enforce_min_pixelsBOOLEANfalse

Outputs (2)

NameTypeDescription
MASKMASK
chosen_resolutionSTRING