NH Mask Aspect Ratio Match
Make one mask's aspect ratio match another's
- target_ratio_mask
- mask_to_adjust
- MASK
Sometimes you don't want to draw a mask - you want this mask to have the same shape proportions as that mask. NH Mask Aspect Ratio Match (class NH_MaskAspectRatioMatch) takes two masks and reshapes one to match the other's bounding-box aspect ratio. If that sounds niche, it's the exact tool for "make my square mask hug the same frame as the garment mask" workflows - the kind of thing the author built it for.
How it works
You provide a target_ratio_mask (the mask whose aspect ratio is the goal) and a mask_to_adjust (the one being reshaped). The node measures the bounding box of each, then rebuilds mask_to_adjust's bounding box to match the target's width:height ratio, keeping the adjust mask's content centered. Three modes decide how the box is fitted:
- stretch - squashes or stretches the adjusted mask's content to the target ratio. Fast, but distorts the content.
- pad - changes the box to the target ratio by adding empty space (padding) around the content. Nothing gets distorted; you just gain background area.
- crop - changes the box by cropping the longer dimension so the box matches the target ratio. You lose some mask edge.
Output is a single MASK at the same resolution as mask_to_adjust.
Where you'd actually use it
Garment and VTON setups mostly: you've got a body or clothing mask, and a separate utility mask (a box, a region) that needs to line up proportionally with it before a composite. It's also handy in portrait work when a face region and a head region need to agree on framing. In the NH pack it sits alongside NH Create Box Mask and Mask Properties (NH) - measure with one, reshape with the other.
Installing
ComfyUI Manager → search NH-Nodes → install → restart. Or:
cd ComfyUI/custom_nodes
git clone https://github.com/jetthuangai/NH-Nodes.git
cd NH-Nodes
pip install -r requirements.txt
Restart and refresh the browser page. numpy/Pillow only.
Gotchas
It operates on the first mask in each input batch - if you feed batches, you get a single-output behavior that may surprise you. An empty mask_to_adjust (all black) is passed through unchanged rather than reshaped, which is safe but can look like the node did nothing. And remember crop actually cuts mask content, so if your mask hugs a subject tightly, pad or stretch are safer than losing the subject's edge. Check the output visually - ratio matching is invisible from the inputs alone.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| target_ratio_mask | MASK | — | |
| mask_to_adjust | MASK | — | |
| mode | COMBO | 3 options: stretch, pad, crop |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| MASK | MASK | — |