ComfyUI Node

BinarizeImage

Threshold it, and the world goes black and white

By keit0728·Created about a year ago·Updated about a year ago· 3
BinarizeImage
  • images
  • IMAGE
threshold127

Binarization is the "no gray allowed" operation: every pixel becomes either pure white or pure black, decided by a single threshold you set. BinarizeImage does exactly that, and it gives you the dial - a threshold value between 0 and 255 that decides the cutoff. It's the tool you reach for when you want hard, flat, 1-bit-ish output: masks, line art, stencil work, document-style output, anything where intermediate values are noise rather than information.

The setup in a real workflow is usually the same. You have a generated image with a background you need to strip cleanly, or a grayscale texture you want to convert into an unambiguous mask. A raw mask at float precision has soft edges and semi-transparent pixels that downstream nodes may or may not handle gracefully. Push it through binarization and you get a decision for every pixel: in or out. No ambiguity.

How it works

The logic is easy to follow even in the source. First the image is converted to grayscale using OpenCV's luminance weighting (the pack's shared helper, which uses cvtColor - roughly 0.299R + 0.587G + 0.114B). Then every pixel whose gray value is at or above threshold / 255 becomes white (1.0) and everything below becomes black (0.0). That binary value is written into all three RGB channels, so the output is a normal 3-channel image of pure black and pure white - no 1-channel tensor surprises for downstream nodes. If the input had an alpha channel, it's preserved.

The one dial that matters:

  • threshold (INT, 0–255, default 127) - the cutoff. Lower it and more of the image turns white; raise it and more turns black. At 127 you get a roughly "lighter than half = white" split.

Input is images (IMAGE), and the output is a single IMAGE tensor. Batches are handled - feed it a stack and every frame gets thresholded.

Where you'd actually use it

The most common real-world use is mask construction: binarizing a luminance or alpha-derived image into a hard in/out mask for compositing, inpainting, or controlnet prep. It's also great for cleaning up scanned-style line art, and it's a standard step in making sprite sheets or pixel-style assets where you want crisp 1-bit boundaries. If your source is a clean document or a flat-colored graphic, BinarizeImage is the straightforward version of this job.

The trap: gradients and JPEG artifacts

A fixed threshold works beautifully on bimodal images - dark text on white paper, a logo on a flat background. It falls apart the moment you have a soft gradient or photographic content, because the threshold has to land somewhere in the gradient and it will, loudly, producing speckle and torn edges. The classic fix: blur (or the pack's AntialiasingImage) before thresholding, or jump to BinarizeImageUsingOtsu in this same pack, which picks the cutoff automatically from the histogram. If you need a soft, feathered mask, don't binarize at all - that's the wrong tool for the job.

Installing

It ships in the keit0728/ComfyUI-Image-Toolkit pack:

cd ComfyUI/custom_nodes
git clone https://github.com/keit0728/ComfyUI-Image-Toolkit

Restart ComfyUI, or install "ComfyUI-Image-Toolkit" via ComfyUI Manager. The only dependency is opencv-python==4.11.0.86, pinned to that exact version - worth knowing because pip may need to move an existing OpenCV install when you add this pack. No models, no GPU, CPU-only.

Gotchas

Beyond the gradient problem, keep in mind this is a tiny anonymous pack (Japanese author, code comments in Japanese, last touched mid-2025). The README's clone command even has a placeholder your-username URL - the real repo is keit0728/ComfyUI-Image-Toolkit. Simple node, works fine, but "support" means reading the source, which is mercifully short.

CategoryComfyUI-Image-Toolkit

Inputs (2)

NameTypeDefaultDescription
imagesIMAGEImages to binarize.
thresholdINT1270–255Values above threshold become white (1.0), below become black (0.0).

Outputs (1)

NameTypeDescription
IMAGEIMAGE