Nodes/comfyui_bmad_nodes/NaiveAutoKMeansColor
ComfyUI Node Runs on cloud

NaiveAutoKMeansColor

Posterize automatically — let the elbow decide how many colors the image actually has

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
NaiveAutoKMeansColor
  • image
  • IMAGE
  • INT
max_k8
rc_threshold0.50
max_iterations100
eps0.20

Color quantization is the trick behind every flat-shaded poster look: reduce a photo to its N most representative colors, and the result reads as stylized vector art. The catch is always the same - how many colors? Pick too few and you crush the detail; pick too many and it looks like a slightly-off photo. NaiveAutoKMeansColor is the "figure it out for me" version of the pack's plain KMeansColor node: it runs the clustering repeatedly, looks for the natural stopping point, and hands you both the posterized image and the number of colors it chose.

The "Naive" in the name is doing honest work. This isn't a fancy model-selection algorithm - it's the classic elbow method on the K-means compactness curve. As you increase k (the color count), the clustering error always drops; it just drops fast at first and then levels off. The "elbow" is the k where the drop stops being dramatic, and that's usually the sensible color count. The node approximates it by finding the sharpest bend in the compactness graph, with a rc_threshold guard so it doesn't chase an angle forever.

The inputs

  • image - what you're quantizing.
  • max_k (3–16, default 8) - the upper bound it searches to. This is the one you'll actually touch: it sets the ceiling on how many colors the result can have.
  • rc_threshold (0.01–1, default 0.5) - the elbow-detection sensitivity.
  • max_iterations (default 100) and eps (default 0.2) - the K-means convergence criteria, passed straight to OpenCV.

The outputs

Two, which is the nice part: the IMAGE (posterized) and an INT - the k it actually chose. That INT is genuinely useful: wire it to a text display to see what it decided, or feed it into your own logic. It's rare to get the "answer" out of a CV node alongside the result, and it makes debugging "why did it pick 5 colors" trivial.

When to reach for it

Stylized collage backgrounds, palette extraction (quantize a reference image and you get its dominant colors as flat regions), and pre-processing for masks where you want clean color bands to segment on. If you want control over the exact color count, use the plain KMeansColor node instead - that one takes number_of_colors directly. This node is for when you don't know the right number and want a reasonable guess.

Installing it

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_bmad_nodes

then restart, or grab comfyui_bmad_nodes in ComfyUI Manager. It's OpenCV-based, so the pack's requirements cover it. No model downloads.

The honest trade-off

Because it's heuristic, the "automatic" k is a guess, and on images with a smooth color gradient it can pick something that looks off. When that happens, don't fight the thresholds - just check what k it chose, and if it's close to what you want, switch to KMeansColor and hardcode it. And note it runs K-means for every k from 1 to max_k on every execution, so on big images it's slower than a single fixed-k call. It's not the tool for real-time tweaking.

CategoryBmad/CV/Color A.

Inputs (5)

NameTypeDefaultDescription
imageIMAGE
max_kINT83–16
rc_thresholdFLOAT0.500.01–1
max_iterationsINT100
epsFLOAT0.20

Outputs (2)

NameTypeDescription
IMAGEIMAGE
INTINT