Get Mask Dimensions
Get Mask Dimensions returns the bounding box, not the canvas size
- mask
- width
- height
- x
- y
The name makes you think "tells me how big my mask is." Read the source and you'll find it does something better: it returns the tight bounding box of the mask's actual content - the non-zero pixels - as a width, a height, and a top-left (x, y). That's the difference between "how large is this canvas" and "where exactly is the stuff in my mask," and the second one is what most real workflows need.
Why you'd reach for it
The classic crop-and-restore pattern. You've got a mask (from a segmenter, an inpaint region, a detection box) and you want to crop the source image to just that region, process it - upscale, detail, regenerate - then paste it back. To crop you need to know where and how big, and those four numbers are exactly the crop box. Feed them to any crop-by-bbox or crop-by-mask node and the pattern falls into place.
How it works
It takes the first image in the mask batch (mask[0]), finds every non-zero pixel with torch.nonzero, and takes the min/max of the coordinates. x and y are the top-left corner of that box; width and height are its extent, computed as max − min + 1, so a single-pixel mask returns 1×1. If the mask has no non-zero pixels at all you get 0, 0, 0, 0 back - no error, just a degenerate box. Safe to feed, but a 0-wide box downstream won't do anything sensible.
Inputs and outputs
One required input: mask (MASK). Four INT outputs: width, height, x, y. That's it. Wire width/height into a crop or resize, x/y into whatever positions the crop, or hand all four to a node that takes a bbox directly.
Traps to keep in mind
This is the bounding box of the mask content, not the mask's own dimensions. A 1024×1024 mask with a small blob in the corner returns the blob's size, not 1024. That's the feature - but if you actually wanted canvas dims, this node isn't it, and there's no separate output for those. It also only looks at the first frame of the mask batch; any additional frames are ignored. And because "non-zero" includes soft anti-aliased edges, a feathered mask returns a box spanning its full falloff - usually what you want for a crop, but worth knowing if you expected razor-tight edges.
Install
From the same ComfyUI_Accessories pack: ComfyUI Manager → search "ComfyUI_Accessories", or:
cd ComfyUI/custom_nodes && git clone https://github.com/var1ableX/ComfyUI_Accessories
then restart. No dependencies, no model downloads.
Common issues
Same pack-wide gotcha as everything here: the GitHub head is currently broken - __init__.py imports a GetRandomDimensions class that was deleted, so the pack won't import until that's fixed (a one-line edit in __init__.py is the workaround). Beyond that, the main "issue" is expectations: an all-black mask returns 0,0,0,0, and a batched mask only reports frame one. If the output looks wrong, check the mask, not this node.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| width | INT | — |
| height | INT | — |
| x | INT | — |
| y | INT | — |