ComfyUI Node Runs on cloud

ToBinaryMask

Turn a soft, grayscale mask into clean black-and-white

By ltdrdata·Created 3 years ago·Updated 4 months ago· 3,248
ToBinaryMask
  • mask
  • MASK
threshold20

Masks generated by SAM, a segmentation model, or hand-painting rarely come out clean. There's antialiasing at the edges, soft partial-alpha pixels, faint noise - values scattered anywhere between 0 and 255 instead of a crisp "in or out." ToBinaryMask collapses that into a real binary mask: every pixel becomes either fully off or fully on, nothing in between.

Per the README's own phrasing, it "separates the mask generated with alpha values between 0 and 255 into 0 and 255. The non-zero parts are always set to 255." In practice, the node's threshold input is what decides the cutoff - pixels at or above the threshold become fully white (255, included in the mask), pixels below it become fully black (0, excluded). It's the same idea as image thresholding in any photo tool, applied to a ComfyUI mask.

Why you'd want this

A lot of downstream operations behave much better on a clean binary mask than a soft, noisy one. Feed a segmentation model's raw output straight into something like MASK to SEGS and you can end up with speckled artifacts or ragged, inconsistent edges - because the mask never really committed to "yes" or "no" for a lot of its pixels. Thresholding it first with ToBinaryMask forces a clean decision everywhere, which makes the resulting SEGS, crop regions, or composite edges much more predictable.

The inputs and output

  • mask (MASK, required) - the soft or noisy mask to clean up.
  • threshold (INT, default 20, range 1–255) - the cutoff. Lower values catch more of the faint, low-alpha pixels and pull them into the mask (a more generous, larger resulting mask); higher values demand stronger alpha before a pixel counts, giving you a tighter, more conservative mask. The default of 20 is deliberately low - it's tuned to catch most of a mask's real body while discarding near-zero noise, not to aggressively shrink it.

Output is a single MASK - now strictly two-valued, ready for anything downstream that wants a clean binary region: MASK to SEGS, a paste/composite step, or feeding a detector.

How to install it

Via ComfyUI Manager: search ComfyUI Impact Pack, install, restart. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/ltdrdata/ComfyUI-Impact-Pack comfyui-impact-pack
cd comfyui-impact-pack
python -m pip install -r requirements.txt   # ComfyUI's python; python_embeded on portable

then restart. No models - this is a plain pixel-value operation.

Common issues & troubleshooting

The mask shrank more than expected. Your threshold is set too high for the mask you're working with - a lot of a soft mask's real content can sit in the lower alpha range, and a high threshold discards it along with the noise. Lower the threshold and re-check.

Edges look jagged now, when the original was smooth. That's the expected trade-off of binarizing - a soft, antialiased edge necessarily becomes a hard step once every pixel is forced to 0 or 255. If you need the mask both clean and soft-edged for compositing, apply a Gaussian Blur Mask node after this one to feather the now-hard edge back out slightly.

Downstream node still behaves oddly on a mask I thought I cleaned. Confirm the ToBinaryMask node is actually wired in before that downstream node, not just present somewhere in the graph - a common mistake is adding the node but leaving the original noisy mask connected directly to the consumer.

CategoryImpactPack/Operation

Inputs (2)

NameTypeDefaultDescription
maskMASK
thresholdINT201–255

Outputs (1)

NameTypeDescription
MASKMASK