Calculate Target Size By Mask [LP]
Turning a mask into width/height numbers
- mask
- height
- width
Feed this a mask, and it hands you back plain height and width integers sized to fit a target resolution while respecting the mask's own aspect ratio. It's a standalone utility version of the same idea the pack's Inpaint Crop pipeline uses internally - except here you get raw numbers you can wire anywhere, not a bundled parameter object built specifically for one other node.
Why you'd want raw numbers instead of the full Inpaint Crop pipeline
If you're actually running the crop-edit-stitch inpainting pattern, you'd normally reach for this pack's InpaintCrop-LP together with one of its dedicated parameter nodes (CroppedAspectSizeParameters-LP and siblings), which package sizing logic into a bundle built specifically to plug into that node. But sometimes you just want the numbers - to size an image-resize node directly, to pass into a completely different pipeline, or to make a sizing decision without adopting the whole crop/stitch workflow. This node does the same aspect-aware size calculation without forcing you into that larger structure.
How it works
It looks at the mask's own bounding-box aspect ratio, then computes a height/width pair that hits your target_size while preserving that ratio - the same "let the program calculate the required dimensions" behavior the README describes for the aspect-size mode of Inpaint Crop. aspect_ratio_limit keeps that calculation from running away on you: a very thin sliver of a mask (a horizontal scratch, a narrow vertical strip) could otherwise imply an absurdly elongated target size, and this caps how extreme the ratio is allowed to get before it's clamped back toward something reasonable.
The inputs and outputs that matter
- mask - the MASK to measure.
- target_size - an INT, default
1024, range 0–16384. The resolution the calculation is aiming to hit. - aspect_ratio_limit - a FLOAT, default
2, range 0–100. Caps how far from square the resulting dimensions are allowed to stretch. - Outputs: height and width, both plain INT values.
How to install it
ComfyUI Manager: search "ComfyUI Level Pixel" or ComfyUI-LevelPixel, install, restart - updates automatically via Manager's "Update ALL." Manual:
cd ComfyUI/custom_nodes
git clone https://github.com/LevelPixel/ComfyUI-LevelPixel.git
Restart ComfyUI. No models needed - it's mask geometry math.
Common issues & troubleshooting
The output dimensions look nothing like your mask's actual shape. Check aspect_ratio_limit first - if your mask is unusually elongated (a thin strip, a long diagonal), the limit may be clamping the result well short of the mask's true proportions on purpose, to keep the target size sane.
An empty or fully-unmasked mask gives odd results. This node needs a mask with an actual masked region to measure a meaningful bounding box from - a blank mask (nothing selected) doesn't have a well-defined aspect ratio to work from, so don't expect useful output from one.
You actually wanted this feeding into InpaintCrop-LP. Use CroppedAspectSizeParameters-LP instead - it does the equivalent calculation but outputs the bundled CROPPEDSIZEDATA type that node's dedicated sizing pin expects, rather than raw height/width integers.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| target_size | INT | 10240–16384 | — |
| aspect_ratio_limit | FLOAT | 2.000–100 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| height | INT | — |
| width | INT | — |