from skimage import io
from sklearn.cluster import KMeans
import numpy as np
image = io.imread('test2.jpg')
io.imshow(image)
io.show()
rows = image.shape[0]
cols = image.shape[1]
image = image.reshape(image.shape[0]*image.shape[1],3)
kmeans = KMeans(n_clusters = 128, n_init=10, max_iter=200)
kmeans.fit(image)
clusters = np.asarray(kmeans.cluster_centers_,dtype=np.uint8)
labels = np.asarray(kmeans.labels_,dtype=np.uint8 )
labels = labels.reshape(rows,cols);
print (clusters.shape)
np.save('codebook_test.npy',clusters)
io.imsave('compressed_test.jpg',labels)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ba695b665b972d42757590b57e35a5e2.png)
image = io.imread('compressed_test.jpg')
io.imshow(image)
io.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4b80feb1d4ae752ae59e3e62eeb033b4.png)