Nodes/ComfyUI-YCNodes/Mask Ratio Pad (YC)
ComfyUI Node

Mask Ratio Pad (YC)

Center your subject in a ratio-perfect frame — pad, crop, and hand back every mask you need

By yichengup·Created 2 years ago·Updated 4 months ago· 48
Mask Ratio Pad (YC)
  • image
  • mask
  • inner_mask
  • image
  • mask
  • ratio_mask
  • pad_mask
  • inner_mask
ratio1:1
pad_modeedge
fill_color#ffffff
extra_expand0
padding_modecenter

The fiddly one, and the most powerful mask node in this pack. Given an image and a subject mask, it frames the subject to a target aspect ratio: it finds the mask's bounding box, builds a ratio-correct rectangle around it, pads the image if the rectangle runs off-canvas, then crops to the rectangle. And it outputs not one but five things, because the pad/crop/restore loop needs all of them.

What it's actually for

When a subject needs to live in a specific canvas - a 16:9 background, a 3:4 card, a square crop for a profile - you don't want to eyeball the crop or hand-align the subject. You want "subject centered, correct ratio, everything else handled." That's this node's job, and the reason it matters for inpainting: the padded border is new pixels you'll want to generate, so it hands you the masks that say exactly where padding was added and where the crop window sits.

How it works

The core loop is simple math. It binarizes the mask (>0.5), finds the bounding box of white pixels, then computes the smallest rect at your target ratio that contains it - anchored center, or to top/bottom/left/right via padding_mode. If the rect spills past an edge, the image is padded outward with the same pad machinery as YCImageSmartPad (edge/reflect/symmetric/wrap/constant). Then it crops to the rect. extra_expand adds a guard band of padding when the crop window touches the canvas edge - the fix for "I centered the subject but there's no room for the model to draw context around it."

The inputs that matter

  • image / mask - subject + its mask
  • ratio - 1:1, 3:4, 4:3, 9:16, 16:9, 3:2, 2:3
  • pad_mode / fill_color - how padded border pixels are generated
  • extra_expand - extra pad when the crop hugs the edge
  • padding_mode - where the subject sits in the frame

Five outputs, and here's what each is for:

  • image - the padded-and-cropped result
  • mask - your original subject mask, padded to match the new image
  • ratio_mask - the final crop rectangle as a mask (white where the crop window is)
  • pad_mask - white exactly where padding was added (your inpaint region)
  • inner_mask - the optional inner_mask input, padded to match

The combo you'll actually use: image + pad_mask into a masked sampler (regenerate only the padded border), then mask + ratio_mask to composite back.

Install

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

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

Restart. Dependencies: torch, numpy, pillow, opencv-python, scipy; no model downloads.

Where people get burned

  • Batch size mismatch throws. If image and mask have different batch counts, it raises a ValueError immediately. Keep them matched.
  • Five outputs is a lot to wire. It's easy to grab the wrong one. Remember: pad_mask = where to inpaint, ratio_mask = the crop window, mask = your subject's original shape.
  • An empty mask panics. If the mask has no white pixels it falls back to the whole image, which is a silent surprise - check your mask before it reaches this node.
  • extra_expand only fires when the window touches the edge. If the subject already sits comfortably inside the frame, it won't add padding - which is correct behavior, but it reads like the knob is broken if you expected unconditional expansion.

This is the node you reach for when a subject has to hit a target ratio without manual placement. It's not the one you'd grab for a five-second crop - it's the one you build a workflow around.

CategoryYCNode/Mask

Inputs (8)

NameTypeDefaultDescription
imageIMAGE
maskMASK
ratioCOMBO1:17 options: 1:1, 3:4, 4:3, 9:16, 16:9, 3:2, +1
pad_modeCOMBOedge5 options: edge, reflect, symmetric, wrap, constant
fill_colorSTRING#ffffff
extra_expandINT00–2000
padding_modeCOMBOcenter5 options: center, top, bottom, left, right
inner_maskoptMASK

Outputs (5)

NameTypeDescription
imageIMAGE
maskMASK
ratio_maskMASK
pad_maskMASK
inner_maskMASK