Nodes/segment anything/InvertMask (segment anything)
ComfyUI Node Runs on cloud

InvertMask (segment anything)

InvertMask (segment anything)

By storyicon·Created 3 years ago·Updated 2 years ago· 1,114
InvertMask (segment anything)
  • mask
  • MASK

It flips a mask. White becomes black, black becomes white, and what was selected is now everything except what was selected. That's the entire node - but it's a genuinely useful little screwdriver, because half the time a segmenter gives you the mask of the wrong side and you just need the opposite.

Why you'd reach for it

Masks are directional: a mask says "this region, not that one." The catch is that the region a tool hands you often isn't the region you want to act on. You run GroundingDinoSAMSegment with the prompt "person" and get a clean mask of the person - but your actual goal is to replace the background, so what you need is the person mask inverted. Or you've isolated a subject for a cutout and now want to blur or relight everything around it. Rather than re-segmenting "the background" (which detectors are bad at - backgrounds aren't objects), you segment the easy thing and flip it.

This is the small connective node that makes a segmentation result reusable in both directions. In our inpainting notes the whole game is regenerating one region while leaving the rest untouched; whether "the rest" means inside or outside your selection is exactly what an invert decides.

How it works

A ComfyUI mask is a grayscale image where each pixel's value is how strongly it's selected - 1.0 (white) is fully in, 0.0 (black) is fully out, and the values in between are partial. Inverting subtracts every pixel from that maximum: full-in becomes full-out and vice versa. Soft, feathered edges invert smoothly too, so a mask with a gentle falloff keeps its gradient, just reversed. Nothing about the shape changes - only which side of it is "on."

The inputs and outputs that matter

There's nothing to tune. One input, mask (MASK), and one output, MASK - the inverted version. You drop it inline between whatever produced the mask and whatever consumes it. No threshold, no options, no surprises.

How to install it

It's part of the segment anything pack, so if you've already got that installed you already have this node. Fresh install - ComfyUI Manager: search segment anything (the storyicon pack), install, restart. Or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/storyicon/comfyui_segment_anything

then pip install -r requirements.txt and restart. Unlike the segment and loader nodes, this one needs no model downloads and no GPU - it's pure array math, so it works even if the GroundingDINO half of the pack failed to build. If the heavy nodes are giving you install grief but you only want a mask flip, this still runs.

Common issues & troubleshooting

Honestly, there isn't much to break here. The one thing worth naming: "invert" only means selected-versus-unselected, not visual polarity. If a downstream node treats black as the active area and white as inactive, inverting can look like it did nothing - or the opposite of what you expected - when really the convention just flipped. If a mask seems backwards after this node, check whether the consumer expects white-is-selected or black-is-selected before assuming the invert misfired.

Two practical notes. Core ComfyUI ships its own InvertMask, and this pack's is functionally the same thing - if you already have a mask-invert in your graph from somewhere else, you don't need this one too. And because inversion is exact, if the flipped result looks wrong it's almost always the input mask that's off (soft where you wanted hard, or grabbing the wrong object) - fix that upstream at the segmenter, not here.

Categorysegment_anything

Inputs (1)

NameTypeDefaultDescription
maskMASK

Outputs (1)

NameTypeDescription
MASKMASK