遮罩填充丨孔洞
Fill the holes in a mask — the fix for every donut-shaped selection
- mask
- mask
- width
- height
Masks from segmentation models have a nasty habit: they leave holes. A subject with an arm looped through a handle, a person holding something in front of them, or a scene where a background sliver shows through the subject - all produce a mask that's technically "the right shape" but has a cavity in the middle. Send that to an inpaint and the sampler happily repaints the hole as background. MaskFill closes those internal holes so your mask becomes one solid, correct region.
It's from ComfyUI-QING (display name "遮罩填充丨孔洞"), and it's the "clean up the mask before it costs you a pass" node in the pack's mask section.
How it works
The mechanism is a textbook binary fill: the mask is binarized at 0.5, then scipy.ndimage.binary_fill_holes fills every enclosed hole - any dark region completely surrounded by selected pixels becomes selected. It's implemented with plain numpy/scipy, runs per-frame over the batch, and returns the filled mask plus its dimensions.
Three inputs, all simple:
mask- the input mask.invert_fill- default false. When true, it fills the outside instead: the result is inverted after filling. The tooltip puts it well - "fill the exterior region rather than the interior holes." Think of it as "make the mask everything except the solid blob."size_info- what the outputwidth/heightreport:original(the full tensor dimensions) ormask_content(default) - the bounding box of the mask's actual content. The content-bbox mode is handy when a mask sits in a tiny corner of a huge canvas and you want to know its true extent.
Outputs: mask (the filled result), plus width and height INTs as determined by size_info.
Where it fits
Any pipeline where a hole in the mask is a bug. The canonical case is inpainting: you want the sampler to preserve the subject and repaint around it, so the mask must be the solid subject - a donut-shaped mask with a hole where the subject's arm forms a loop would let the background bleed in. Fill it, and the inpaint operates on a clean region. Same for detailers and for compositing - a solid mask pastes cleanly; a holed one leaves artifacts.
The invert_fill mode is the sleeper use case: sometimes what you actually want is "everything except this blob" - a background mask. One toggle and the fill becomes the inverse selection.
The honest take
It's a tiny node with a single real algorithm behind it, and that's fine - binary_fill_holes is the correct tool for the job and there's no reason to reinvent it. The two design decisions worth appreciating are the size_info toggle (content-bbox is genuinely useful) and invert_fill (turns a one-mode node into two). Where it can't help: holes that are open to the edge of the mask aren't "enclosed," so binary_fill_holes semantics mean a notch that runs to the mask's border won't be filled - that's the boundary condition to know about.
Installing
Part of ComfyUI-QING. ComfyUI Manager: search "ComfyUI-QING". Or:
cd ComfyUI/custom_nodes
git clone https://github.com/GAO-SHIQING/ComfyUI-QING
cd ComfyUI-QING
python install_dependencies.py # or: pip install -r requirements.txt
Restart ComfyUI. It needs scipy (in requirements.txt) for the fill - that's the one dependency to make sure got installed. No model downloads. China mirror: --mirror --auto. Python ≥ 3.9.
Gotchas
An empty or zero-element mask raises a ValueError rather than returning gracefully - feed it something. The fill only fills enclosed holes, as noted; if your mask has a gap that reaches the canvas edge, you'll need a different approach (morphological closing, or MaskBlend's expansion). And remember the 0.5 binarization: a very soft mask gets rounded to hard edges before filling, so if you need the feathering preserved, run this before feathering, not after.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | 输入遮罩 | |
| invert_fill | BOOLEAN | false | 反转填充:填充外部区域而非内部孔洞 |
| size_info | COMBO | mask_content | 尺寸信息:original(原始张量尺寸) 或 mask_content(遮罩内容区域尺寸) |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |
| width | INT | — |
| height | INT | — |