Nodes/comfyui_imgutils/Mask Combine
ComfyUI Node

Mask Combine

Union, subtract, and everything between

By LK-168·Created about a year ago·Updated about a year ago· 1
Mask Combine
  • mask1
  • mask2
  • mask
operation
mask1_weight1.00
mask2_weight1.00
clamp_resulttrue

Two masks in, one mask out - with every operator you'd want

Once you start generating masks from multiple sources you immediately need to combine them, and "combine" is a loaded word. Union them? Overlay them? Cut one out of the other? Mask Combine gives you seven operations plus per-mask weights, so instead of hunting for a different node per operation you get all of it behind one operation dropdown.

How it works

Two masks in, one out. The operations are pixel-wise arithmetic on the mask values:

  • add - union. Both regions, summed (values can exceed 1 until clamped).
  • subtract - mask1 minus mask2. The classic "everything except" - remove the hand mask from the person mask.
  • multiply - intersection (for binary masks, 1×1 = 1 only where both are on).
  • divide - mask1 / mask2 (with a tiny epsilon to avoid division by zero). Rarely used, occasionally just what you need for ratio tricks.
  • max / min - element-wise maximum or minimum. max is another way to union, min another way to intersect; they're the "no surprise clamping" versions.
  • xor - symmetric difference: (A + B) − 2·(A·B). On/off where exactly one mask is on. Great for "this or that, not both."

The supporting knobs: mask1_weight / mask2_weight (0–2) scale each mask before the operation - fade a soft SAM mask in or out of the combine - and clamp_result (default on) pins the result to [0,1]. If the two masks have different sizes, the smaller is resized to match the larger (so no shape-mismatch errors, though resizing means your alignment matters).

Inputs that matter

mask1, mask2, and operation are required; the weights are the optional ones. The use I keep coming back to: face mask + hand mask from SAM, add them, feed the union into a detailer so it refines both at once. The inverse - person mask minus hand mask, subtract - keeps the hand out of the inpaint region so it doesn't get regenerated.

Install

Part of LK-168/comfyui_imgutils. ComfyUI Manager → search "comfyui_imgutils", or:

cd ComfyUI/custom_nodes
git clone https://github.com/LK-168/comfyui_imgutils

Restart ComfyUI. Pure tensor math - no models, no downloads, no env vars.

Common issues

  • Values look wrong after add. Two overlapping 1.0 masks add to 2.0; that's what clamp_result is for. Leave it on unless you genuinely want unclamped values.
  • Different-size masks. It resizes rather than errors, so the smaller mask gets stretched to fit - if they're from different sources, eyeball the alignment or resize upstream yourself.
  • Division surprises. Dividing by a near-zero mask value explodes; the epsilon guards it, but the results are rarely intuitive. Reach for it deliberately, not experimentally.

A single node that covers every combine you'll realistically need - that's the whole appeal.

Categoryimgutils/mask

Inputs (6)

NameTypeDefaultDescription
mask1MASK
mask2MASK
operationCOMBO7 options: add, subtract, multiply, divide, max, min, +1
mask1_weightoptFLOAT1.000–2
mask2_weightoptFLOAT1.000–2
clamp_resultoptBOOLEANtrueClamp result to [0,1] range

Outputs (1)

NameTypeDescription
maskMASK