Nodes/IMGLAB_ComfyUI/Auto Mask (IMGLAB)
ComfyUI Node

Auto Mask (IMGLAB)

The mask-picker node that saves you from hand-wiring resolution checks

By IMGLAB-net·Created 5 months ago·Updated 5 months ago· 2
Auto Mask (IMGLAB)
  • image
  • mask_1
  • mask_2
  • mask_3
  • mask_4
  • mask_5
  • mask_6
  • mask_7
  • mask_8
  • else
  • image
  • mask
  • rgba
multi_match
no_match

If you've ever wired a mask into an inpainting or compositing workflow and watched it do nothing because its resolution didn't match your canvas, this is the node that got you. Auto Mask (IMGLAB) takes one image plus up to eight masks, compares every mask's width and height against the image's, and hands you the first one that matches - exact pixel-for-pixel, no resampling guesswork. It's a tiny router, and routing is the whole job.

ComfyUI's masking story is messy precisely because masks are resolution-obsessed. A mask from a background-removal model like BiRefNet comes out at whatever you fed it; a mask you saved and reloaded lands at its stored size; an upscale in between silently changes the canvas while your mask stays stuck at the old dimensions. Inpainting and alpha compositing both fail when that happens - the mask just doesn't line up, or the sampler ignores it entirely. Auto Mask's trick is to treat the image as the source of truth and only accept masks that agree with it.

How it works

Under the hood it's dead simple, and that's the appeal. For each connected mask_N slot the node checks height == image_height and width == image_width. Matching masks are collected, and the lowest-numbered slot always wins - if mask_1 and mask_3 both fit, mask_1 gets picked. Masks get batch-expanded to match the image's batch size, so a single mask feeds a whole batch of images fine.

Then come the two combo widgets, which are where the actual policy lives:

  • multi_match - what to do when two or more masks fit. Use First Match silently takes the lowest slot (the default, and honestly what you want 90% of the time); Raise Error halts the queue and lists the conflicting slot numbers so you can go clean up your graph.
  • no_match - what to do when nothing fits. Output Empty Mask is the default and gives you a zeroed mask (your RGBA comes out fully transparent - the silent-failure trap, more below); Use Else Mask reveals a fallback else input slot; Raise Error stops and tells you which sizes were actually connected.

One subtlety the README doesn't scream about: the else fallback gets resized to the image via nearest-neighbor interpolation if it doesn't match, unlike the regular mask slots which must match exactly to count. That makes else your safety net - throw a same-ratio mask in there and it'll be stretched to fit, even if your canvas moved.

The outputs

Three outputs, each with a clear job: image is a direct passthrough of the input, mask is the winning mask (or the empty/else fallback), and rgba is your image with that mask fused in as the alpha channel, shaped [B, H, W, 4]. The rgba output is the star for background-removal pipelines - you get a ready-to-composite transparent PNG straight out, no separate "image + alpha" bookkeeping.

Installing it

The pack is one node and has zero Python dependencies - no requirements.txt, nothing to download, just pure torch and a frontend script. ComfyUI Manager has it under IMGLAB_ComfyUI, or drop it in by hand:

cd ComfyUI/custom_nodes
git clone https://github.com/IMGLAB-net/IMGLAB_ComfyUI

Restart ComfyUI and you'll find "Auto Mask (IMGLAB)" in the IMGLAB category. The node adds some genuinely nice UI while it's at it: resolution labels on every slot - grey for the image, green ✓ / red ✗ per mask after a run - a ? button in the title bar with an in-graph help panel, and mask slots that appear as you connect them and collapse when you don't. Mask slots are dynamic up to mask_8.

Where people get burned

The no_match = Output Empty Mask default is the classic footgun. It never errors - it just quietly hands you a blank mask and a fully transparent RGBA, so the "fix" appears to do nothing and you debug the wrong half of your graph. If you're setting this up fresh, flip no_match to Raise Error until you've verified your masks actually match, then decide what the fallback should be. Second gotcha: matching is exact, so a mask that's off by one pixel from an upscaled image doesn't match - that's not a bug, that's the intended strictness, and it's exactly why the else slot exists. And third, remember rgba is a four-channel image for compositing downstream - don't feed it into a VAE decode expecting a normal latent-sized input.

Honest verdict: this is a small, focused utility from a brand-new pack (a single commit, so treat it as early days), but the mechanic is sound and it removes a genuinely annoying class of "why is my mask not doing anything" failures.

CategoryIMGLAB

Inputs (12)

NameTypeDefaultDescription
imageIMAGE
multi_matchCOMBO2 options: Use First Match, Raise Error
no_matchCOMBO3 options: Output Empty Mask, Use Else Mask, Raise Error
mask_1optMASK
mask_2optMASK
mask_3optMASK
mask_4optMASK
mask_5optMASK
mask_6optMASK
mask_7optMASK
mask_8optMASK
elseoptMASK

Outputs (3)

NameTypeDescription
imageIMAGE
maskMASK
rgbaIMAGE