├── README.md ├── codebook_tiger.npy ├── compressed_tiger.png ├── image_decompress.py ├── img_compress.py ├── reconstructed_tiger.png └── tiger.png /README.md: -------------------------------------------------------------------------------- 1 | # Image-compression-with-Kmeans-clustering: Color Quantization 2 | 3 | It uses K-means clustering to compress an image without affecting the quality much. It is to illustrate an application of K-means clustering algorithm in quantizing the colors present in the original image in only 'k' distinct colors. 4 | 5 | Best thing would be to follow my blog-post for implementation. The description about compression of '.png' images with K-means and the steps to follow can be read from my blog: 6 | 7 | https://appliedmachinelearning.wordpress.com/2017/03/08/image-compression-using-k-means-clustering/ 8 | 9 | I have uploaded one sample image of tiger in png format. The path used in the compress.py and decompress.py file can be checked accordingly. 10 | 11 | It is a python implementation using Scikit-learn ML library. 12 | -------------------------------------------------------------------------------- /codebook_tiger.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhijeet3922/Image-compression-with-Kmeans-clustering/eb2de31da121c446725af427816068454e0a8161/codebook_tiger.npy -------------------------------------------------------------------------------- /compressed_tiger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhijeet3922/Image-compression-with-Kmeans-clustering/eb2de31da121c446725af427816068454e0a8161/compressed_tiger.png -------------------------------------------------------------------------------- /image_decompress.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from skimage import io 4 | import numpy as np 5 | 6 | centers = np.load('codebook_tiger.npy') 7 | 8 | c_image = io.imread('compressed_tiger.png') 9 | 10 | image = np.zeros((c_image.shape[0],c_image.shape[1],3),dtype=np.uint8 ) 11 | for i in range(c_image.shape[0]): 12 | for j in range(c_image.shape[1]): 13 | image[i,j,:] = centers[c_image[i,j],:] 14 | 15 | io.imsave('reconstructed_tiger.png',image); 16 | io.imshow(image) 17 | io.show() 18 | -------------------------------------------------------------------------------- /img_compress.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from skimage import io 4 | from sklearn.cluster import KMeans 5 | import numpy as np 6 | 7 | image = io.imread('tiger.png') 8 | io.imshow(image) 9 | io.show() 10 | 11 | rows = image.shape[0] 12 | cols = image.shape[1] 13 | 14 | image = image.reshape(image.shape[0]*image.shape[1],3) 15 | kmeans = KMeans(n_clusters = 128, n_init=10, max_iter=200) 16 | kmeans.fit(image) 17 | 18 | clusters = np.asarray(kmeans.cluster_centers_,dtype=np.uint8) 19 | labels = np.asarray(kmeans.labels_,dtype=np.uint8 ) 20 | labels = labels.reshape(rows,cols); 21 | 22 | np.save('codebook_tiger.npy',clusters) 23 | io.imsave('compressed_tiger.png',labels); 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /reconstructed_tiger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhijeet3922/Image-compression-with-Kmeans-clustering/eb2de31da121c446725af427816068454e0a8161/reconstructed_tiger.png -------------------------------------------------------------------------------- /tiger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhijeet3922/Image-compression-with-Kmeans-clustering/eb2de31da121c446725af427816068454e0a8161/tiger.png --------------------------------------------------------------------------------