Nodes/Pixel Grid Helpers/Pixel Grid: Quantize (Max Colors)
ComfyUI Node

Pixel Grid: Quantize (Max Colors)

Shove any image into N colors and see which ones actually matter

By molbal·Created 9 months ago·Updated 9 months ago· 3
Pixel Grid: Quantize (Max Colors)
  • image
  • quantized_image
  • hex_palette
max_colors16

Quantize (Max Colors) is the workhorse of Pixel Grid Helpers: give it an image and a number, and it reduces the image to that many colors, then hands you back both the result and the palette it used, as a hex string. It's the node that actually answers "what are the important colors here?" instead of just counting them.

Why you'd reach for it: it's the on-ramp to the whole pixel-art pipeline. Run your messy diffusion output through this at 16 colors and you get a posterized version that exposes the piece's structure, plus a ready-made palette string you can feed straight into Enforce Palette or Palette to Image. It's also just satisfying as a stylization tool - 8 colors gives you a flat graphic look, 2–4 gives you a logo.

How it works is worth knowing, because it explains the node's one genuine quirk. It's a hand-rolled K-Means in pure torch: pick max_colors random pixels as starting centroids, assign every pixel to its nearest centroid, move each centroid to the mean of its assigned pixels, and repeat for 10 iterations. The source comments that ten is usually plenty for pixel art - and that's the kind of honesty you want. The quirk: the centroids start from random pixels, so the result varies run to run. Same image, same settings, subtly different palette each time. There's no seed input on this node. Usually harmless; if you need repeatability, feed the output palette into Enforce Palette to lock it in.

Two more source-grounded notes. The distance metric is plain Euclidean RGB, not perceptual, so the colors that "win" aren't always the ones your eye would pick. And watch the memory story: the distance matrix is pixels × max_colors, so running maxed out at 256 colors on a huge image eats a gigabyte-plus of tensor. Run it on the downscaled pixel-art version, not the 4K original - which you'd be doing anyway, since the median fixer gives you the small clean version.

Inputs: image, and max_colors (1–256, default 16). Outputs: quantized_image (IMAGE) and hex_palette (STRING, comma-separated). The palette always reports its full quota of codes - when a cluster empties out mid-training, the code re-seeds it from a random pixel rather than shrinking the count, so don't assume every code on the list is a real dominant color.

Install: identical to the rest of the pack. ComfyUI Manager → search "Pixel Grid Helpers" → install, restart, refresh. Or git clone https://github.com/molbal/ComfyUI-PixelGridHelpers.git into custom_nodes. No models, no dependencies - it's all tensor math, and it shows.

CategoryPixel Grid Helpers

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
max_colorsINT161–256

Outputs (2)

NameTypeDescription
quantized_imageIMAGE
hex_paletteSTRING