Nodes/ComfyUI-YCNodes/Resize Mask To Ratio (YC)
ComfyUI Node

Resize Mask To Ratio (YC)

Pad your mask's content out to a clean aspect ratio — no canvas resize involved

By yichengup·Created 2 years ago·Updated 4 months ago· 48
Resize Mask To Ratio (YC)
  • mask
  • resized_mask
ratio1:1
padding_modecenter

The name says "resize," but Resize Mask To Ratio (YC) doesn't resize the canvas at all. It looks at the white content in your mask, finds its bounding box, and draws a rectangle around it at a target aspect ratio - 1:1, 3:4, 4:3, 9:16, 16:9, 3:2, or 2:3. The output is the same size as the input, with a filled rectangular region sized so the original content sits inside a region of your chosen ratio.

Why would you want that? Because diffusion and edit models are ratio-sensitive, and a mask region that's, say, 10:1 is a great way to get a model to behave strangely. If you're building an inpaint region, a composite box, or a conditioning area that has to conform to a model's preferred framing - or you're making a portrait crop that must be exactly 3:4 - this normalizes the region before it hits anything that cares. It's the "make my region ratio-clean" utility, and there's surprisingly little else that does exactly this.

How it works

It binarizes the mask, finds the bounding box of all white pixels, and computes a target rectangle that contains that box at the requested ratio. If the content is wider than the ratio allows, the height grows; if it's taller, the width grows - the content always stays fully inside. Then it fills the rectangle with white on a blank mask of the original canvas.

padding_mode controls where that rectangle sits relative to the content: center (centered on the content), top_left (anchored to the content's top-left, extending right and down), or bottom_right (anchored to the content's bottom-right, extending up and left). center is the default and the one you'll use 90% of the time. The rectangle is clipped to the canvas, so content near an edge just gets less padding on that side.

Output

One MASK, resized_mask, same dimensions as the input. Pair it with a crop - crop to this mask and you get a ratio-clean image without guessing coordinates.

Install

Part of ComfyUI-YCNodes. ComfyUI Manager → search "ComfyUI-YCNodes", or:

cd ComfyUI/custom_nodes
git clone https://github.com/yichengup/ComfyUI-YCNodes

Restart ComfyUI. Standard deps, no downloads. Under "YCNode/Mask".

Gotchas

An empty mask returns a blank mask (which is the graceful move, but don't feed it nothing and wonder where the box went). The rectangle grows to contain the content, never shrinks, so if your content is already a wider ratio than the target, the target rectangle will be bigger than the content - that's the point, but it means "resize to 1:1" doesn't crop your content down, it pads it up. For cropping to a ratio, chain this into a crop node instead.

CategoryYCNode/Mask

Inputs (3)

NameTypeDefaultDescription
maskMASK
ratioCOMBO1:17 options: 1:1, 3:4, 4:3, 9:16, 16:9, 3:2, +1
padding_modeCOMBOcenter3 options: center, top_left, bottom_right

Outputs (1)

NameTypeDescription
resized_maskMASK