Nodes/ComfyUI-HappNodeSet/Color Quantizer
ComfyUI Node

Color Quantizer

Eight colors can fix a pixelated mess — and give you a palette worth stealing

By mikemojen·Created 8 months ago·Updated 3 months ago· 0
Color Quantizer
  • image
  • quantized_image
  • color_palette
num_colors8
quantization_methodkmeans
ditheringnone
color_spaceLAB
sample_fraction0.5
smoothing_iterations0

Color Quantizer reduces an image to 2, 4, 8, or 16 colors. On paper that sounds like you're destroying information; in practice it's one of the best cheap fixes for pixelated diagrams, banding, and color noise. When a generated graphic has millions of muddy intermediate colors, reducing it to a handful of dominant ones makes it look intentional - flat poster colors instead of a smear. And as a bonus it hands you the palette itself as an image output, which is genuinely handy when you're building a style guide or a material swatch for a workflow.

How it works

It finds the num_colors dominant colors in the image, then maps every pixel to its nearest palette color. The knobs that matter:

  • num_colors - 2, 4, 8 (default), or 16. Fewer colors = flatter, more posterized. Start at 8.
  • quantization_method - kmeans, median_cut, most_frequent, or kmeans_minibatch. kmeans is the default and the best quality-for-size tradeoff; kmeans_minibatch is noticeably faster on big images with slightly rougher palettes; most_frequent is dumb and fast; median_cut is the classic quantizer. For a diagram, kmeans rarely disappoints.
  • dithering - none or floyd_steinberg. Dithering scatters noise to fake intermediate shades, which is great for photos and terrible for clean flat graphics. For diagrams, leave it none.
  • color_space - RGB or LAB (default). LAB separates lightness from color, so quantizing in LAB keeps lines crisp and colors true. This is the default for a reason; switch to RGB only if LAB's output feels off.

The optional sample_fraction (default 0.5) limits how many pixels the clustering algorithm looks at - lower it for speed on huge images, and smoothing_iterations (0–5) blurs out speckle in the final result, useful after aggressive dithering or noisy input.

What comes out

Two outputs: quantized_image (the flat poster result) and color_palette (a visualization of the chosen palette that matches the image's dimensions). That palette output is the sleeper feature - wire it to a Preview and you've got an instant swatch you can eyeball without digging through the image.

Where it fits

Use it before the pack's cleanup nodes: quantize → Extract Black or Non-White to Black → LineDetector gives you much cleaner line detection on noisy source, because the background is now a single flat color instead of 300 shades of off-white. For pixel-art-upscale workflows it's a standard pre-step, and for anything going to print or engraving, flat colors are what the machinery expects.

Install

Part of ComfyUI-HappNodeSet by mikemojen. Manager: search HappNodeSet. Manual:

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

Restart ComfyUI. The clustering uses scikit-learn - which is not in the pack's requirements.txt (that lists numpy, opencv-python, scipy, scikit-image, svgwrite, Pillow, torch, cairosvg). If you don't already have scikit-learn installed for another node, expect an ImportError on first run and pip install scikit-learn it. That's the one dependency gotcha in this pack.

Honest take

For photos, this is a toy - real quantization for images belongs to a proper post-processing step, and most_frequent mode will make your landscape look like a budget Game Boy game. But for flat graphics, UI mockups, and diagram cleanup, it's exactly right, and the palette output alone justifies having it installed.

Categoryimage/preprocessing

Inputs (7)

NameTypeDefaultDescription
imageIMAGE
num_colorsCOMBO84 options: 2, 4, 8, 16
quantization_methodCOMBOkmeans4 options: kmeans, median_cut, most_frequent, kmeans_minibatch
ditheringCOMBOnone2 options: none, floyd_steinberg
color_spaceCOMBOLAB2 options: RGB, LAB
sample_fractionoptFLOAT0.50.1–1
smoothing_iterationsoptINT00–5

Outputs (2)

NameTypeDescription
quantized_imageIMAGE
color_paletteIMAGE