Machine learning / Python · K-means · RGB

Image compression with K-means.

An image quantization project exploring how a smaller color palette changes the way an image looks and can be represented.

View repository ↗

Fewer colors, a different trade-off

Each pixel is treated as a point in three-dimensional RGB space. K-means groups similar colors, then replaces each pixel with its cluster’s representative color. The number of clusters controls the size of the resulting palette.

Concepts explored

  • RGB pixels as three-dimensional data
  • Clustering and representative colors
  • The trade-off between palette size and visual detail

Project notes

A smaller palette reduces the number of colors needed to represent the image. Actual file-size savings depend on the encoding and metadata; the palette alone does not guarantee a smaller file.

A smaller palette, up close

A simple color illustration: change the palette size to see how a color ramp loses detail. This uses evenly spaced levels, not the project’s K-means algorithm.

8 representative colors

Next projectA shell in C ↗