Mpi Mask Square Bbox
Turn any mask into a centered square crop — with x, y and size
- mask
- square_mask
- x
- y
- size
Squares are annoying. You find a subject with a mask - a face, a car, a logo - and you want to crop a clean square around it for an inpaint pass or a fixed-aspect generation, but the mask's bounding box is almost never square, and a naively square crop runs off the image edge. Mpi Mask Square Bbox does the fiddly geometry for you: it takes a mask, works out the tight bounding box of its nonzero pixels, and expands it to a centered square that's clamped - shrunk if necessary - to stay fully inside the image. Out comes a filled square_mask plus the x, y, and size you'd need to reproduce that crop with any crop node.
Two inputs, one is obvious: mask in, and padding, an INT (default 0) that adds pixels around the tight mask box before squaring - the "give the subject some breathing room" knob. The outputs are square_mask (a full-resolution mask, black with a white square at the region), x and y (top-left corner), and size (side length). If the mask is empty - no nonzero pixels at all - it returns a blank mask and zeros for x, y, and size, so a downstream crop node gets a harmless no-op instead of an out-of-bounds crash.
The geometry is worth understanding because it's what makes the node trustworthy. It computes the tight box, pads it, then sets the square side to the longer edge of the box, capped at the image's smaller dimension. The square is centered on the box's center, then clamped into the image. The consequence: a tall skinny mask (80×10) becomes an 80×80 square centered on the same spot; a mask whose square would overflow the 50-pixel-tall image is shrunk to 50 so it still fits. In every case the result is inside the canvas, which is the property you actually want from a crop region.
Where it fits in a workflow: pair it with a crop that takes x/y/size, or feed it into the pack's MPI_BOX-aware nodes if you want the region as a single wire (the pack's MpiBoxCrop takes an MPI_BOX - x/y/size maps onto it cleanly). The classic use is inpainting - mask a face, square it, crop, regenerate, paste back. Because the square is guaranteed inside the image and the x/y/size come out as data, this is also the node a host app would use to build a crop for you, and it's why the Mad Pony pack (the engine behind Cubric Vision) ships it as a first-class ImgOps utility.
Install is the pack's standard no-drama story: ComfyUI Manager → search "ComfyUi-MpiNodes" → install → restart, or
cd ComfyUI/custom_nodes
git clone https://github.com/MadPonyInteractive/ComfyUi-MpiNodes
No dependencies, no models - pure tensor math. If you've ever hand-fiddled crop coordinates to avoid an out-of-bounds square, this node is the "stop doing that" answer.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| padding | INT | 00–4096 | Pixels added around the tight mask box before squaring. |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| square_mask | MASK | — |
| x | INT | — |
| y | INT | — |
| size | INT | — |