Coders Packet

Image Compression in Python using K-Means Clustering

By Nihal Chandra

Implementation of the K-Means Clustering algorithm to compress images by keeping only the most useful 'k' colors of an image.

Image Compression, as the name suggests, is used to reduce the size/quality of an image so that it can be stored and transmitted conveniently. Each image consists of smalls cells called pixels, and each such pixel has an associated color with it. These pixels together form the image. For example, the image used in this project consists of 900 x 637 = 5,73,300 pixels. 

The human eye is generally able to differentiate between pixels of lower order well but as the number of pixels keep increasing, though the quality of image technically increases, there doesn't seem to be any significant change to the human eye. We use this property to our advantage by picking up the 'k' most prominent colors in an image and alter the remaining pixels to one of these clusters so that overall, there isn't much loss in image quality.

Mainly two operations are performed in K-Means algorithm until convergence - Association of a pixel with a cluster and updation of centroid by calculating the mean of the centrods. To analyze the results, we will look at different values of k and observe the corresponding scatter plot and compressed image. From the results, we observe that for k >= 16, there isn't any significant difference in the quality of the image.

The image used in this project for demonstration is obtained from Illumination Entertainment images.

This project has been implemented in Python 3.6 environment using Jupyter Notebook by making use of the following libraries:

- Numpy                   v1.19.1

- Matplotlib               v3.3.1

- Sci-kit learn            v0.23.2

Implementation:

Open the 'Image Compression in Python using K-Means Clustering.ipynb' file using Jupyter Notebook and click on the Run button |>>| 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Nihal Chandra (nihalchandra)

Download packets of source code on Coders Packet