Nodes/Masquerade Nodes/Mask To Region
ComfyUI Node Runs on cloud

Mask To Region

Turn a blob mask into a clean crop box

By BadCafeCode·Created 3 years ago·Updated 2 years ago· 479
Mask To Region
  • mask
  • IMAGE
padding0
constraints
constraint_x64
constraint_y64
min_width0
min_height0
batch_behavior

A mask from ClipSeg, SAM, or a hand-painted brush is an arbitrary blob - it can be any shape, any size, sitting anywhere in the frame. Most of what you want to do with it next (crop it out, resample it at a proper resolution, feed it to a model with size requirements) needs a rectangle instead. Mask To Region is the node that does that conversion: give it a blob, get back a clean rectangular region sized to contain it, with whatever padding and size rules you need.

This is the node that makes full-resolution inpainting actually work well. The idea, echoed by basically every "only masked" inpainting workflow since: instead of resampling your whole 1024x1024 image to fix a small masked area, find the smallest sensible rectangle around that area, crop just that, resample it at full resolution, then paste it back. Mask To Region computes that rectangle. It's the same job the newer Inpaint Crop node does for modern inpainting workflows - this pack had the pattern early.

The inputs that matter

  • mask - the mask to compute a bounding region for.
  • padding (INT, default 0) - extra margin added around the mask's bounding box, so your resample region isn't cropped exactly at the mask's edge (which tends to produce hard seams). 32–64px is a reasonable starting point for face/detail work.
  • constraints - how the final rectangle's dimensions get decided: keep_ratio locks the region to the aspect ratio you set with constraint_x/constraint_y; keep_ratio_divisible does the same but also forces the width and height to divide evenly into those values (useful for models that need dimensions to be multiples of something); multiple_of ignores the ratio and just rounds width/height up to the nearest multiple of constraint_x/constraint_y; ignore skips all of this and just uses the padded bounding box as-is.
  • constraint_x / constraint_y (default 64 each) - the numbers constraints uses, in whichever way that mode interprets them.
  • min_width / min_height - floor values; the region grows to meet these if the mask's natural bounding box is smaller.
  • batch_behavior - only matters with more than one mask at once: match_ratio keeps every region the same aspect ratio and rescales all masks to the largest one's size (bicubic); match_size grows every region to match the largest one exactly.

Output is a single mask representing the resultant rectangular region - this is what you feed into Cut By Mask to actually crop the image.

If you're batching (batch size greater than one), use keep_ratio rather than ignore. Without it, differently-shaped masks in the same batch produce differently-shaped regions, and when Cut By Mask later resizes everything to match the largest one, unconstrained regions get visibly stretched.

Installing it

Mask To Region comes with Masquerade Nodes. Install through ComfyUI Manager (search "Masquerade Nodes") or manually: cd ComfyUI/custom_nodes && git clone https://github.com/BadCafeCode/masquerade-nodes-comfyui, then restart ComfyUI. Pure geometry - no models, no extra dependencies. The pack overall is a small, dependency-free library predating ComfyUI Manager; its README points at Impact Pack as the better-maintained general alternative for masking and segmentation, but the crop/region math here is simple enough that "better maintained" mostly isn't the deciding factor for this particular node.

Common issues

The confusing part for most people is the interaction between padding, min_width/min_height, and the constraints mode - it's easy to end up with a region bigger than you expected because two of those are fighting to satisfy different minimums at once. If your output region is unexpectedly huge, check min_width/min_height first; they're absolute floors and will override a tightly-cropped mask. If your crop is landing on the wrong part of the image after resizing, that's almost always a batch-size mismatch further downstream - Cut By Mask resizes everything to the largest crop when sizes vary, so an unconstrained (ignore) region in a batch can throw off alignment even though Mask To Region itself computed the box correctly.

CategoryMasquerade Nodes

Inputs (8)

NameTypeDefaultDescription
maskIMAGE
paddingINT00–1048576
constraintsCOMBO4 options: keep_ratio, keep_ratio_divisible, multiple_of, ignore
constraint_xINT642–1048576
constraint_yINT642–1048576
min_widthINT00–1048576
min_heightINT00–1048576
batch_behaviorCOMBO2 options: match_ratio, match_size

Outputs (1)

NameTypeDescription
IMAGEIMAGE