Image Compression Using “K-Means Clustering”

Sakshi Mahajan
Analytics Vidhya
Published in
3 min readApr 28, 2020

--

I always wonder it would be nice if without using an andriod application everytime i can compress images using 2–3 steps in my jupyter notebook only. So, i figured out that k-means clustering can be used for this one time setup and removing all the future delays and frustrations while fillinf up forms or in any registrations processes.

With just 3 steps i can compress my image as per requirement:

  1. Upload image for compression.
  2. Select pixel or size requirements.
  3. Download the compressed image.

This one time process saved me a lot that’s why i am here to share this with you.

Let’s begin with what exactly is k-means clustering? how it will help to compress images? why we using this process for compression of images?

Introduction:

The K-Means algorithm clusters data by trying to separate samples in n groups of equal variance, minimizing a criterion known as the inertia or within-cluster sum-of-squares (see below). This algorithm requires the number of clusters to be specified. It scales well to large number of samples and has been used across a large range of application areas in many different fields.

The k-means algorithm divides a set of N samples X into K disjoint clusters C, each described by the mean μj of the samples in the cluster. The means are commonly called the cluster “centroids”; note that they are not, in general, points from X, although they live in the same space.

The K-means algorithm aims to choose centroids that minimise the inertia.

Steps:

  1. Import all libraries: numpy, matplotlib, sklearn, ipywidget.
  2. Data Preprocessing: image is ploted and dimensions are fixed and reshaped.
  3. Visualizing the Color Space using Point Clouds

4. Visualizing the K-means Reduced Color Space.

5. K-means Image Compression with Interactive Controls.

This steps finally results in required outputs for further deatils you can check my jupyter notebook.

Results:

Reference:

--

--

Sakshi Mahajan
Analytics Vidhya

React Developer | Frontend Developer | Data Science Enthusiast