Nodes/ComfyUI_BaiKong_Node/BK Img To Color
ComfyUI Node

BK Img To Color

Pulls the palette out of any image with K-means

By JayLyu·Created 2 years ago·Updated about a year ago· 8
BK Img To Color
  • input_image
  • COLORS
  • SELECT_COLOR
num_colors1
get_complementary_colorfalse
accuracy80
exclude_colors
select_color1

You've got a reference image whose color mood you want to steal - a photograph, a painting, a frame from a film. BK Img To Color extracts the dominant colors straight out of the image and hands them to you as hex strings you can feed anywhere. If you only install one node from the ComfyUI_BaiKong_Node pack, this is the one.

It's the pack's most genuinely useful color node: real palette extraction via K-means clustering, entirely CPU-side, no models to download. The output plugs straight into the pack's other color nodes - grab a palette from a reference, then push a single color through BK Color Contrast to find readable text for it, or BK Color Limit to tame it.

How it works

Under the hood it's scikit-learn's KMeans. The node flattens the image into a pile of RGB pixels (dropping any alpha channel), clusters them into num_colors groups, and takes each cluster's center as a "dominant color." A couple of details matter:

  • Colors are sorted by dominance - the biggest cluster first. So index 1 is the color that actually covers the most pixels, not the most interesting one.
  • accuracy is a quality dial, not a count: it maps to K-means iterations and restart attempts (max_iter = 512 × accuracy/100, and n_init scales with it). Lower accuracy is fast and sloppy; 100 is slower but more stable. For a still image you barely need to crank it - 80 is the default for good reason.
  • The result is deduplicated (case-insensitively) and can be filtered with exclude_colors, a comma-separated list of hex values you never want in the output.
  • get_complementary_color inverts each extracted color as RGB - (255-r, 255-g, 255-b). Go in with eyes open: that's a flash-negative inversion, not a tasteful hue-rotated complement. It's fine for finding a guaranteed contrasting accent; it will not hand you a "matching" color wheel opposite.

Two outputs come back: COLORS - the full list as a comma-separated hex string - and SELECT_COLOR, the single color at your select_color index (1-based, clamped to the last color if you overshoot). That single string is what you'll wire into a text overlay or a gradient.

Inputs

  • input_image - any image.
  • num_colors - how many dominant colors (1–20, default 1).
  • accuracy - 1–100 quality slider.
  • get_complementary_color - bool, default off.
  • exclude_colors - comma-separated hex to skip.
  • select_color - which extracted color to return as SELECT_COLOR.

Install

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

Restart ComfyUI, or search "ComfyUI_BaiKong_Node" in ComfyUI Manager. This node is the reason scikit-learn is in the requirements - it's the only dependency doing real heavy lifting here.

Gotchas

Since the colors are dominance-sorted, select_color=1 gives you the most common color, which is often an unremarkable background tone rather than the striking accent you actually noticed. If you're hunting the "pop" color, bump num_colors up and scan a couple of indices. Also remember the complement is a raw RGB inversion - for a palette that looks designed, disable it and pick from the extracted list instead. If every color you wanted gets excluded, you'll get a gray fallback rather than an error, which is honest but easy to miss.

Category⭐️ Baikong/Color

Inputs (6)

NameTypeDefaultDescription
input_imageIMAGE
num_colorsoptINT1
get_complementary_coloroptBOOLEANfalse
accuracyoptINT801–100
exclude_colorsoptSTRING
select_coloroptINT1

Outputs (2)

NameTypeDescription
COLORSSTRING
SELECT_COLORSTRING