Get Mask Bounding Box
Measure what the mask actually covers
- mask
- mask
- xLeft
- yTop
- width
- height
- tinyrect
Segmentation masks are great at telling you where something is, but they're not coordinates - they're a big array of ones and zeros. When you need actual numbers - the x, y, width and height of the region the mask covers - Get Mask Bounding Box extracts them. It takes a mask and returns the tight bounding box around its painted area, plus a solid rectangular version of that box as a mask.
The use case is auto-cropping and reframing. A segmenter or inpainting setup produces a mask of the subject; this node measures the subject and hands you the geometry to crop precisely to it. It also does the reverse: because the returned mask is a solid rectangle, it gives you a clean rectangular region you can feed to nodes that want a mask-shaped input rather than raw coordinates. The padPct input even lets you grow the box around the subject for breathing room - or shrink it.
How it works
It scans the mask for any pixel above zero, finds the min/max coordinates of that region across the whole batch, and reports them as xLeft, yTop, width, and height. Then it builds a new mask that's all ones inside that box and zeros outside, and returns it. The padPct float (default 0, range −100 to 100) expands or contracts the box by that percentage of its smallest dimension - positive pads out, negative tightens in - always clamped to the image bounds.
Inputs: mask (MASK) and padPct (FLOAT).
Outputs: mask (the solid rectangle version), xLeft, yTop, width, height - the integer geometry you can wire into any crop node.
Installing it
Get Mask Bounding Box is part of ComfyUI-TinyBee, the small MIT-licensed utility pack from TinyBeeman. No models, no heavy deps. Via Manager:
- ComfyUI Manager → Custom Nodes Manager.
- Search "ComfyUI-TinyBee" and install.
- Restart ComfyUI.
Or clone:
cd ComfyUI/custom_nodes
git clone https://github.com/TinyBeeman/ComfyUI-TinyBee
Restart and it's under 🐝TinyBee/Util. Standard library plus torch/PIL, all of which ComfyUI already provides.
Gotchas
The output mask is a rectangle, not your original shape. If you pass a blob-shaped segmentation mask in and expect the same blob back, you'll get a box around it - that's by design, and it's what makes the node useful for cropping, but it's a footgun if you're not expecting it. Also, padPct pads by a percentage of the smallest dimension, so a wide flat mask gets proportionally less margin than a square one. And if the input mask is entirely empty, you get zeros out - a crop node downstream will happily crop nothing, so guard that case if it matters.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| padPct | FLOAT | 0.0-100–100 | — |
Outputs (6)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |
| xLeft | INT | — |
| yTop | INT | — |
| width | INT | — |
| height | INT | — |
| tinyrect | TINYRECT | — |