ZML_遮罩相加减
Add and subtract masks like they're numbers
- 遮罩1
- 遮罩2
- 输出遮罩
Masks are just numbers between 0 and 1, so it's a small crime that ComfyUI doesn't ship an obvious add/subtract node for them. ZML_MaskAddSubNode (ZML_遮罩相加减) fixes that gap with about twenty lines of code: 遮罩1 and 遮罩2 in, one 输出遮罩 out, and an 操作 dropdown choosing 相加 (add) or 相减 (subtract).
The mechanism is exactly what you'd hope. Both masks are clamped into 0–1. For add it computes clamp(mask1 + mask2), so overlapping white regions stay white instead of over-blooming to 2. For subtract it computes clamp(mask1 - mask2), so mask2 carves out of mask1. If the two masks have different resolutions, the node resizes the second to match the first with bilinear interpolation before the math - one of those "did they really handle that?" details that saves you a silent shape error.
When is this actually the tool you reach for? The mask-detection world is full of "I have two masks and I need the union or the difference." Add is the union: a face mask plus a hand mask becomes one "fix both" region for a detailer pass. Subtract is the exclusion: a subject mask minus a background mask, or "everything except the face" for an inpaint that should leave the face alone. It's the plumbing that sits between whatever produced your masks and whatever consumes them - cheap, deterministic, no model involved.
The practical notes are few. The operation is per-pixel and stateless - there's no preview, no feathering, no blur. If your masks have soft edges, add and subtract do math on soft edges and that's fine, but they won't smooth anything for you; pair with a blur or erode node if seams matter. Also, the resize-on-mismatch only handles the "second differs from first" case - it always conforms mask2 to mask1, so if mask1 is the smaller one you lose mask2's resolution, and it resizes the whole mask rather than cropping or padding, which can shift alignment. For same-size masks from the same detector, none of that matters.
Install is pack-standard - ComfyUI Manager search "ComfyUI-ZML-Image", or:
cd ComfyUI/custom_nodes
git clone https://github.com/zml-w/ComfyUI-ZML-Image
then restart. It sits under ZML 图像 → 遮罩. No dependencies beyond torch, which you already have. This is one of those nodes you don't think about until you need it, and then you need it constantly - if you do any multi-mask work at all, the five minutes to install it are worth it.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| 遮罩1 | MASK | — | |
| 遮罩2 | MASK | — | |
| 操作 | COMBO | 2 options: 相加, 相减 |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| 输出遮罩 | MASK | — |