Color quantization: compressing an image to K colors
Computer vision / image compressionA photograph stored in 24-bit color can contain up to million distinct colors. Displaying or transmitting it cheaply calls for a much smaller palette while keeping the image visually faithful.
Treat each pixel as a point in 3D RGB space and run K-Means with over the pixels. Each centroid becomes a palette color, and every pixel is recolored to its nearest centroid — minimizing the within-cluster squared color distance, which is exactly perceived reconstruction error in RGB.
The palette collapses from ~16.7 million possible colors down to 64 colors — a reduction in distinct colors — while the image remains visually almost indistinguishable from the original. Storing a 64-color palette needs only 6 bits per pixel for the index versus 24 bits for full color, roughly a 4x reduction in raw pixel storage. This is the canonical color-quantization demo shipped in scikit-learn.
Source: scikit-learn: Color Quantization using K-Means — scikit-learn developers