Nodes/ComfyUI 1hewNodes/Mask To BBox Mask
ComfyUI Node

Mask To BBox Mask

Turn a mask into its bounding boxes — but as masks, and 8-aligned

By 1hew·Created about a year ago·Updated 7 days ago· 33
Mask To BBox Mask
  • mask
  • bbox_mask
output_modemerge
divisible_by8

Some operations don't care about your subject's exact silhouette - they want the box around it. Mask To BBox Mask takes a mask, finds the tightest rectangle(s) around the foreground, and outputs those rectangles as masks, with sizes rounded up to a multiple of 8. That last bit is the whole trick: a lot of downstream nodes (inpainting, attention control, tiling) are happiest when regions are even numbers, and 8-aligned boxes are the de-facto standard in ComfyUI land.

What it's for

Reach for it whenever you want to "snap" a region to a rectangle - cropping a subject out with room to spare, building an inpaint region that's bigger and more forgiving than the mask itself, or feeding a regional node a clean box instead of a ragged edge. The 8-divisibility isn't decoration; models and VAE-style pipelines that downscale in 8×8 blocks will thank you.

How it works

Same foundation as the pack's Mask Separate: the mask is thresholded and connected components are found via scikit-image's label/regionprops. Then each region's bounding box is expanded outward (centered, so it grows in both directions) until its width and height are multiples of divisible_by, clamped to the canvas. Finally the box is drawn as a solid rectangle mask.

output_mode decides the granularity:

  • merge (default) - one box that encloses all foreground regions. One rectangle around everything.
  • separate - one box per connected region, sorted reading-order, each becoming its own mask in the output batch.

The divisible_by control (default 8) is the alignment knob; leave it unless you have a specific need for 16 or 4. One output: bbox_mask (MASK).

The inputs that matter

  • mask (MASK) - the foreground to box in.
  • output_mode - merge vs separate, above.
  • divisible_by (INT, default 8) - alignment granularity for box dimensions.

Installing it

ComfyUI Manager → search ComfyUI-1hewNodes → install → restart, or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/1hew/ComfyUI-1hewNodes

Restart ComfyUI. No model downloads - numpy + scikit-image only (the pack's requirements add heavier things like rembg and ultralytics that install regardless). Pack uses the newer comfy_api.latest API, so update ComfyUI if nodes don't appear.

Common issues

The most common surprise is that expanding-to-multiple doesn't always look "neat": because the box grows from the center, a region near the canvas edge can't expand as far on that side, so two boxes that should be identical widths can differ. That's the clamp working as designed, not a bug. If a mask has scattered specks, separate mode will happily make one box per speck - run it through Mask Separate's min_area filtering first, or merge mode is your friend. And note the output is a filled rectangle mask, not a crop: pair it with an image crop node if you want the actual pixels.

Category1hewNodes/mask

Inputs (3)

NameTypeDefaultDescription
maskMASK
output_modeCOMBOmerge2 options: merge, separate
divisible_byINT81–1024

Outputs (1)

NameTypeDescription
bbox_maskMASK