ComfyUI Node

Extract Black

The fastest way to get a clean black-on-white line drawing

By mikemojen·Created 8 months ago·Updated 3 months ago· 0
Extract Black
  • image
  • image
black_threshold0.15
invert_outputfalse

Extract Black is the "good enough, now" node in this pack. It takes an image and converts everything that isn't black to pure white, leaving the black linework intact on a clean background. If you're feeding messy diagrams into a line detector, a skeletonizer, or the pack's RasterToUniformSVG, this is often the first node you want in the chain - because every downstream tool in this pack assumes it's getting dark lines on a light background, and most scanned or AI-generated diagrams violate that assumption.

How it works

Dead simple, and honest about it: for each pixel it takes the maximum of the R, G, and B channels. If that max is below black_threshold, the pixel counts as black and keeps its original value. Everything else becomes white. Using the max channel rather than an average means a pixel only has to have one channel in the dark range to count - which is exactly what you want for anti-aliased black text, where edges grade into gray. Pixels with alpha (RGBA inputs) keep their alpha channel untouched, so transparency survives.

The one decision that matters: black_threshold (0–1, default 0.15). At the default, only genuinely dark pixels survive. Scanned diagrams are often grayish rather than true black - creases, low DPI, and printer dithering make the "lines" hover around 0.3–0.4 brightness - so bump the threshold to catch them. Set it too high and you'll start swallowing gray fills and shadows you wanted gone. There's no auto mode; this is a manual knob and it rewards a quick visual check.

invert_output (default off) flips to a black background with white lines, which some downstream consumers prefer.

Where it fits

Chain it as Load Image → Extract Black → LineDetector and suddenly the line counter sees what you see instead of a confetti of background colors. It's the naive sibling of Extract Black Advanced - Advanced adds luminance and saturation logic and multiple output modes for edge cases; this one trades all that for a single slider you'll understand in five seconds. For most clean-ish input, this is the one I'd reach for.

Install

Part of ComfyUI-HappNodeSet by mikemojen. In ComfyUI Manager search HappNodeSet and install, or:

cd ComfyUI/custom_nodes
git clone https://github.com/mikemojen/ComfyUI-HappNodeSet.git
pip install -r ComfyUI-HappNodeSet/requirements.txt

Restart ComfyUI afterward. Dependencies are the pack standard - numpy, OpenCV, scipy, scikit-image, Pillow, torch, svgwrite - which your environment very likely already has from other nodes.

Common issues

If everything turns white, your black_threshold is too low for the image - raise it. If the output still looks gray and mottled, you're capturing background noise; lower the threshold. If your lines are actually colored (blue ink, for instance), this node won't help - it's the black/white conversion only, and you want Extract Red or a color-specific filter first. And if the output looks identical to the input, you're probably feeding it a pure black-and-white image that was already clean - which, frankly, is the best possible outcome. This node exists to make downstream measurement trustworthy, so preview the result once before trusting any line counts that come after it.

Categoryimage/processing

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
black_thresholdFLOAT0.150–1Pixels with RGB values below this threshold are considered black (0.0 = pure black only, higher = more tolerance)
invert_outputoptBOOLEANfalseIf enabled, outputs black background with white lines instead

Outputs (1)

NameTypeDescription
imageIMAGE