Nodes/DazzleNodes Collection/Fit Mask to Image
ComfyUI Node

Fit Mask to Image

The Node That Ends Mask Size Mismatch Pain for Good

By DazzleNodes·Created 10 months ago·Updated 5 months ago· 4
Fit Mask to Image
  • image
  • mask
  • latent
  • fixed_mask
  • preview_image
  • info
  • masked_latent
missing_maskpass_through

Somewhere in every inpainting workflow's history is this error: your mask is 958×958, your KSampler output is 960×960, and nothing matches. It happens because samplers round to dimensions divisible by 8, so a mask you drew on the original image no longer lines up with what came out of the last pass. Fit Mask to Image is the one-node fix that replaces the standard 10-node chain people used to assemble for this.

The math is boring in the best way: it reads the dimensions off your image, scales the mask to exactly match, and hands back a fixed_mask you can wire straight into SetLatentNoiseMask or an inpainting sampler. The author ships this as a single node that replaces a documented 10-node workflow (mask → image → scale → image → mask, plus VAE/merge steps), which should tell you how much of a ritual this used to be.

What you actually set

Three inputs matter, and two of them are trivial:

  • image - the reference. This is what the mask gets scaled to match.
  • mask - the mask that's the wrong size.
  • missing_mask - what to do if the mask comes in empty. pass_through (default) does nothing, all_visible and all_hidden generate a full or empty mask at image size, error raises. If you've ever had an empty mask silently wreck a run, set this to error while debugging.

Optionally feed a latent and you get masked_latent out the fourth slot - that's the SetLatentNoiseMask step folded in, so you can skip a node there too.

Why the scaling method matters

It resizes with nearest-exact interpolation rather than bilinear. That's not a technicality: bilinear scaling feathers mask edges, which bleeds generation outside the area you actually wanted and leaves soft seams. Nearest-exact keeps the boundary sharp, so the mask stays an honest "change exactly here" region. For masked inpainting, where the whole point is keeping the unmasked pixels untouched, that's the difference between a clean composite and a blurry halo.

The three useful outputs are fixed_mask (the corrected mask), preview_image (your image with the mask overlaid as an alpha channel, so you can eyeball the fit), and info (a string with the old and new dimensions - genuinely handy when you're chasing down why a workflow broke).

The real-world failure this kills

The exact scenario people hit in the wild: an empty mask from a preview/mask-editing node comes through with shape [H, W] instead of [B, H, W], and downstream inpainting-crop nodes interpret it as a batch of a few thousand single-pixel masks - suddenly you get "64 images" out of a crop node and you're sure the pack is cursed. The community answer when that thread went around was literally "add a node that scales the mask to the image size." That node is this one.

Installing it

It's part of the DazzleNodes collection - search DazzleNodes in ComfyUI Manager, or:

cd ComfyUI/custom_nodes
git clone --recursive https://github.com/DazzleNodes/DazzleNodes.git

Restart ComfyUI and look under DazzleNodes → Fit Mask to Image. No model files, no extra Python deps - it uses ComfyUI's built-in torch/PIL/numpy.

One last tip from experience: check the info string when you first wire it in. If the original mask is already the right size, it tells you that too, which means you can confirm whether the mismatch is upstream (a mask node resized it) or downstream (a sampler rounded it) without guessing.

CategoryDazzleNodes

Inputs (4)

NameTypeDefaultDescription
imageIMAGE
maskMASK
missing_maskCOMBOpass_through4 options: pass_through, all_visible, all_hidden, error
latentoptLATENT

Outputs (4)

NameTypeDescription
fixed_maskMASK
preview_imageIMAGE
infoSTRING
masked_latentLATENT