Nodes/Prompt Helpers/Mask to Bounds
ComfyUI Node

Mask to Bounds

Mask to Bounds

By Pauan·Created 8 months ago·Updated 5 months ago· 3
Mask to Bounds
  • mask
  • x
  • y
  • width
  • height

Sometimes you don't need to know where a face or subject is - you need a rectangle that ComfyUI can act on. That's the whole job of Mask to Bounds: it looks at a mask, finds the smallest box that contains every non-black pixel, and hands you four integers. It's the tiny plumbing node that sits between "I have a mask" and "crop/re-render this exact region."

The classic use is the automatic detailing loop. A detector (GroundingDINO + SAM, a face detector, whatever) produces a mask; a detailer needs a bounding box to crop, upscale, re-render, and paste back. Mask to Bounds is the bridge that derives the box from the mask instead of making you type x/y/width/height by hand or eyeball them on a preview. If you're hand-painting masks in Photoshop and manually typing crop coordinates, this node is the upgrade.

How it works

Nothing clever, which is the appeal. It runs torch.nonzero() on the mask to find every pixel that isn't black, takes the min/max of the x and y coordinates, and reports the box. Empty mask (all black) returns 0, 0, 0, 0 rather than crashing, which is a sensible degenerate case. One thing worth knowing: it returns the bounding box of the mask, not a polygon. If your mask is a thin crescent or a donut shape, the box is the rectangle around the whole thing - for cropping purposes that's usually exactly what you want.

Inputs and outputs

One input, four outputs, no settings to fiddle with:

  • mask (MASK) - any mask; it can come from an inpaint mask, a SAM/GroundingDINO segment, or a hand-painted one.
  • x, y, width, height (all INT) - the box. These wire straight into crop nodes, ImageCrop-style inputs, or the pack's own EZ Crop / regional rendering nodes.

It's a pure data node, so the output is deterministic - feed it the same mask and you always get the same box, which makes it a handy source for anything that wants a BOUNDING_BOX-style rectangle without four loose wires.

Install

This is one node in the Prompt Helpers pack by Pauan. ComfyUI Manager → search "Prompt Helpers" → install, or:

cd ComfyUI/custom_nodes
git clone https://github.com/Pauan/comfyui-prompt-helpers

Then restart ComfyUI. The pack installs only PyYAML and desktop-notifier - no models, no downloads. It needs ComfyUI 0.8.0+ because the pack uses the new Nodes 2.0 extension API; if nothing appears after install, update ComfyUI first.

Where people get burned

  • A fully black (or fully transparent) mask gives 0, 0, 0, 0. That's not a bug - it's an empty box. If your detector missed its target, the downstream crop gets a zero-size region and the graph can fail weirdly or silently do nothing. Filter or check before it feeds a crop.
  • Feathered masks. If your mask is soft-edged, the "non-black" threshold means the box hugs the solid core, not the feather. If you want the crop to include the feather, it's worth growing the box or the mask before hand.
  • It returns the box of all non-black pixels - noise specks or a stray white dot in a corner will stretch the box to include them. Clean the mask first if that matters.

This is a quiet utility node, the kind that never shows up in a headline - but once you've rebuilt the "mask → crop → detail → paste" loop manually, you'll recognize it as the missing link. It's the node that lets a workflow find a face, crop to it, and fix it, all without you typing a single coordinate.

Categorymask

Inputs (1)

NameTypeDefaultDescription
maskMASK

Outputs (4)

NameTypeDescription
xINT
yINT
widthINT
heightINT