反转遮罩
Flip a Mask So Background Becomes Foreground — Exactly One Number
- mask
- mask
KSInvertMask does one thing: out = 1.0 - mask. Foreground becomes background, background becomes foreground, and every gray value in between flips too. That's the whole node, and honestly that's fine - mask inversion is one of those operations you reach for constantly in masking workflows, and a dedicated node keeps the graph readable.
Why you'd use it
The canonical case in this pack's pipeline: after SAM gives you a foreground mask for the product, you often want a background mask - for inpainting, for conditioning a generator to only touch the background, or for telling a compositing step "everything that isn't the product." KSApplyMaskBackground handles the white/transparent side itself, but anything else (an inpaint pass on the backdrop, a background replacement model) wants the inverted mask. That's this node's job.
It's also useful as a sanity check. Wire the mask into a preview before and after inversion and you can see at a glance whether your mask is actually covering what you think it is - a half-inverted mask in the preview is the fastest way to catch a "my SAM prompt matched the background" mistake.
The inputs
Just one: mask. A single MASK tensor, from any mask-producing node in the pack (SAM segmentation, KSLoadImageWithPath's alpha channel, or any other pack's mask output - MASK is a standard ComfyUI type, so this node interoperates freely, unlike the pack's private SAM/GROUNDING_DINO types). Output is the inverted mask, same shape, same batch size.
Installing
With the pack:
cd ComfyUI/custom_nodes
git clone https://github.com/kongshan4219/ComfyUI-Kongshan-Nodes
restart ComfyUI. No models, no keys, no dependencies beyond numpy.
Gotchas
Honestly, there aren't many. The only real traps are conceptual:
- It's not binary. The node flips grays too (
1 - value), so a soft-edged mask stays soft-edged, just mirrored. If you wanted a hard binary inversion, threshold the mask first (themask_thresholdknob on the pack's background nodes does exactly that). - Don't invert when you don't need to. Some nodes expect a "keep" mask and some expect a "mask this out" mask; the convention varies by pack. If your compositing looks wrong after adding this node, remove it and check whether the downstream node actually wanted the foreground.
A one-input, one-output utility node in a pack whose star attractions are the detect/segment pipeline and the API group. It's the kind of node that makes a workflow readable - you read "Invert Mask" on the canvas instead of decoding a math primitive. That's its whole value, and it delivers it.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | 待反转遮罩。输入值 1 会变为 0,输入值 0 会变为 1,中间灰度也会按 1-value 反转。 |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |