Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Artificial Intelligence(AI) & Machine Learning(ML) Training in Jaipur
Online Training - Youtube Live Class Link
0 like 0 dislike
in Artificial Intelligence(AI) & Machine Learning by Goeduhub's Expert (2.1k points)
edited by

In this article, k-means clustering unsupervised learning algorithm using scikit-learn and Python to build an image compression application.

An image is collection of pixels having intensity values between 0 to 255. Color component of a image is combination of RGB(Red-Green-blue) which requires 3 bytes per pixel

In this application reducing the number of colors required to show the image from multiple unique colors to 64, while preserving the overall appearance quality.

Goeduhub's Online Courses @Udemy

For Indian Students- INR 570/- || For International Students- $12.99/-


Course Name

Apply Coupon


Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon


Computer Vision with OpenCV | Deep Learning CNN Projects

Apply Coupon


Complete Machine Learning & Data Science with Python Apply Coupon


Natural Language Processing-NLP with Deep Learning in Python Apply Coupon


Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon


Complete Python Programming from scratch with Projects Apply Coupon

1 Answer

0 like 0 dislike
by Goeduhub's Expert (2.1k points)
edited by
Best answer

Image Compression using K-Means Clustering Unsupervised Machine Learning

Now days, we have huge amount of data in form of images and producing lots of images in our day to day life. People upload lots of images daily on social media platforms like Instagram, Facebook, twitter and other cloud storage. 

To deal with huge amount of data , we want to store image data as efficiently as possible to maximize image quality and minimize  storage space and processing resources, Idea came into existence is image compression.

Formally, image compression is the type of data compression applied to digital images to reduce their cost of storage or transmission. 

What is k-means clustering?

First we should have knowledge of k-means clustering. K-means clustering is technique to group similar objects into one cluster. For example,Companies such as Amazon, Netflix, all group their customers on the basis of their interest and their search history etc, and then invite their customers to buy the product. Another example like we are having a dataset that contains the location of people from all over the world, then we can create different clusters according to different states, such that each cluster contains people of a particular state only.

Implementation of Image Compression using K-means

In this application reducing the number of colors required to show the image from multiple unique colors to 64, while preserving the overall appearance quality.

As we know that an image consists of different colours, so while compressing the image using K-Means Clustering we will create clusters of major colours and group all the similar colors in one cluster, forming different clusters for only major colors.

K-means clustering will group similar colors together into ‘k’ clusters (say k=64 in this case) of different colors (RGB values). Therefore, each cluster centroid is the representative of the three dimensional color vector in RGB color space of its respective cluster. You might have guessed by now how smoothly K-means can be applied on the pixel values to get the resultant compressed image. Now, these ‘k’ cluster centroids will replace all the color vectors in their respective clusters. Thus, we need to only store the label for each pixel which tells the cluster to which this pixel belongs. Additionally, we keep the record of color vectors of each cluster center.  Following original and reduced image-

original and compressed images using kmean image compression

Importing necessary libraries- 

#data science librires

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.cluster import KMeans

 Reading and plotting with the help of matplotlib library-

img = plt.imread("1.jpg")



Checking data type of an image in system- its always numpy array.



Analyzing the properties of image-



Output- (176, 287, 3) 


The image shape contains the rows( 176), columns(287) and channels(3) in the image. In our image 3 channels because it is coloured image. If we check for grayscale image then colour channel is only 1.

Image size means total no. of pixels (rows * columns * channels).

Reshape image and Normalize image pixel values-

#3d to 2D image by combining w and h


image_array = img.reshape(w*h, d)


#normalize in the range of (0,1)


 Output- (50512, 3)

Convert image from 3D to 2D by combining rows and columns, now image contains only 2 parameters that is number of pixels and number of channels. To normalize image pixel value , divide the image size by 255 because that is the maximum intensity value for RGB individually.

Extracting small subset of image for training model-

from sklearn.utils import shuffle

# fitting model on a small sub sample of the complete image

image_array_sample = shuffle(image_array,random_state=1)[:1000]


Output- 3000

KMeans Model Creation and training-


Predicting labels for complete image-

#get labels all centroids on the complete image

labels = kmeans.predict(image_array)

Printing Centroids- 



Recreate Original image according to labels-

#recreate original image according to labels and each pixels

def recreate_image(c,labels,w,h,d):



#now label each pixels according to the limited labels

  for i in range(w):

    for j in range(h):





Visualizing and comparing the original and compressed images- 









original and reduced image by kmeans 

Artificial Intelligence(AI) Training in Jaipur 

Machine Learning(ML) Training in Jaipur 

Our Mentors(For AI-ML)

Sharda Godara Chaudhary

Mrs. Sharda Godara Chaudhary

An alumna of MNIT-Jaipur and ACCENTURE, Pune


Ms. Nisha

An alumna of IIT-BHU


About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © Social::   |  |