# color compression using k-means algorithm

At ln[22] here author mentioned that Input color space is 16 million possible colors. How author came up with 16 million number here. Kindly explain.

Additionally at the end it is mentioned as below

Some detail is certainly lost in the rightmost panel, but the overall image is still easily recognizable. This image on the right achieves a compression factor of around 1 million! While this is an interesting application of k-means, there are certainly better way to compress information in images. But the example shows the power of thinking outside of the box with unsupervised methods like k-means.

How compression is done here as we are still using data of shape (273280, 3) after compression. How compression of 1 million is achieved.

Thanks

• The 16 million possible colors is because images using the RGB system (en.wikipedia.org/wiki/RGB_color_model). So each color is a combination of Red (256 possible variants), Green (256 possible variants) and Blue (256 possible variants). 256 * 256 * 256 = 16 777 216. – Geert Immerzeel Nov 5 '19 at 14:56

As already pointed out in a comment, the 16 million is the number of colors in the RGB space, $$256^3=16\ 777\ 216$$. (The image itself only contains $$96\ 615$$ unique colors.)
As for the reduction, the author is generating 16 clusters; while the center points are still given in RGB format, there are only 16 of them. (If you're following along in Colab or your own clone, run np.unique(new_colors, axis=0); that gives the 16 unique colors remaining in the compression.) Now, to actually save the color-compressed image with smaller disk usage, you'd probably need to convert those 16 RGB codes with simple label encoding, and be able to reference their colors upon loading. Anyway, as the author points out this isn't really meant to be an effective way to reduce image size, just an interesting example of clustering.