Nodes/NH-Nodes/NH Mask-Aware Resize
ComfyUI Node

NH Mask-Aware Resize

Resize an image and never lose the mask's subject

By jetthuangai·Created 5 months ago·Updated 21 days ago· 6
NH Mask-Aware Resize
  • image
  • mask
  • image
  • mask
width512
height512
mode
pad_color_hex#000000

Plain resize nodes don't care what's in your image - they scale the whole frame and let the subject land wherever it lands. NH Mask-Aware Resize (class NH_MaskResizeImage) is the version that does care: it resizes an image/mask pair to a target size while keeping the masked subject centered and fully inside the result. You give it a target resolution, and the subject comes along intact. That's the entire pitch, and it's genuinely useful.

How it works

You feed image and mask (both from the same source, same resolution) plus target width and height. The node finds the mask's bounding box, then picks a scale and placement so the subject ends up centered in the output. Two modes:

  • crop - scales up until the target size is filled, then crops around the centered subject. Nothing gets letterboxed; you lose image edges instead. This is the "fill the frame with my subject" mode.
  • pad - scales just enough that the subject fits inside the target, then fills the leftover area with pad_color_hex (default black). No content is lost; you gain a border.

Both output the resized image and the correspondingly transformed mask, kept in perfect sync - which is the whole point of the node, since resizing the two separately always drifts.

Where you'd actually use it

The classic case: you've detected a face or a garment with a mask, and you want to feed a centered, standard-resolution crop to an inpaint or detailer pass - the "zoom in on the mask, upscale, re-render" loop from the detailing playbook. The mask-aware centering means your subject isn't randomly offset in the frame, which genuinely changes how well a re-render comes out. It's also handy for feeding a VTON pipeline a subject-aligned image instead of a raw full-frame one.

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

pad_color_hex only matters in pad mode - in crop mode it's ignored, which is fine but easy to forget when debugging a black border that "won't go away." Also note the default target is 512×512 and steps by 8, matching the latent-friendly convention; keep your target on the 8-step grid or you'll fight resizing twice. And if your mask is all-black or empty, there's no subject to center - behavior degrades toward a plain resize, so check the mask is valid before relying on the centering.

CategoryNH-Nodes/Image

Inputs (6)

NameTypeDefaultDescription
imageIMAGE
maskMASK
widthINT51264–8192
heightINT51264–8192
modeCOMBO2 options: crop, pad
pad_color_hexSTRING#000000

Outputs (2)

NameTypeDescription
imageIMAGE
maskMASK