Nodes/ComfyUI Segment Anything/InvertMask (segment anything plus)
ComfyUI Node

InvertMask (segment anything plus)

The mask flip that turns 'keep the subject' into 'keep everything else'

By un-seen·Created 2 years ago·Updated 2 years ago· 11
InvertMask (segment anything plus)
  • mask
  • MASK

A mask says "these pixels are the subject." Half the time what you actually need is the opposite: "everything that is not the subject." That's this node. InvertMask takes a MASK and returns its inverse - every 1 becomes 0 and every 0 becomes 1, computed as 1.0 - mask under the hood. No settings, no dropdown, no model. One input, one output.

Sounds trivial, and it is. But it's the difference between two very different workflows built on the same segmentation, and that's why it ships in the pack.

What you use it for

GroundingDinoSAMSegment gives you the mask of the thing your prompt found. Feed that mask into InvertMask and you have the "everything else" mask - which is exactly what you want when the goal is to keep the subject and regenerate around it:

  • Regenerate the background while keeping the person: invert the person mask, feed the inverted mask to an inpaint pass, and the sampler redraws the surroundings while the subject pixels stay untouched. This is masked inpainting's whole remaining advantage over whole-image edit models - bit-identical pixels outside the mask - and the inverted mask is how you aim it.
  • Protect a region from a detail pass. Invert the face mask and everything except the face becomes the editable zone.
  • Swap semantics between composite tools. Some nodes treat a mask as "where to apply", others as "where to hide". When a downstream node wants the opposite convention from what SAM produced, one invert fixes it instead of redrawing anything.

Wiring it up

The output is a MASK of the same shape as the input, so it drops into any input that accepts a mask: Set Mask conditioning, an inpaint crop node, a composite. The common chain is:

Load Image → GroundingDinoSAMSegment (MASK) → InvertMask → Inpaint / Set Mask

If you're coming from a workflow that used a background-removal node and felt like it fought you, this is often the missing step: the remover hands you a subject cutout, and you had to think backwards to use it. Here the invert is explicit and visible in the graph.

Installing

It's the same pack as the other four nodes - ComfyUI Manager (search "ComfyUI Segment Anything"), or:

cd ComfyUI/custom_nodes
git clone https://github.com/un-seen/comfyui_segment_anything_plus

No extra dependencies and no models to fetch just for this node; if the pack is installed for segmentation, this is already there.

One honest warning

Because it's a pure 1 - mask, it inverts everything - including antialiased edges. A soft mask edge becomes a soft edge on the other side, which is usually fine, but if you invert repeatedly (mask → invert → invert) you won't get your original back byte-for-byte at the boundaries. For typical mask work nobody notices; just don't use it as a no-op placeholder in a critical chain. And if you see another InvertMask in your node list from a different pack (WAS Node Suite has one), they're interchangeable for this purpose - grab whichever is loaded.

Categorysegment_anything_plus

Inputs (1)

NameTypeDefaultDescription
maskMASK

Outputs (1)

NameTypeDescription
MASKMASK