K-means算法应用:图片压缩

时间:2023-12-21 16:06:44
plt.imshow(china[:,:,2])
plt.show()

from sklearn.datasets import load_sample_image
china=load_sample_image('china.jpg')
print(china.shape)
china

K-means算法应用:图片压缩

import matplotlib.pyplot as plt
plt.imshow(china)
plt.show()

K-means算法应用:图片压缩

plt.imshow(china[:,:,0],plt.cm.gray)
plt.show()

K-means算法应用:图片压缩

K-means算法应用:图片压缩

from sklearn.datasets import load_sample_image
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np china=load_sample_image("china.jpg")
plt.imshow(china)
plt.show() image=china[::3,::3]
image.shape plt.imshow(image)
plt.show() x=image.reshape(-1,3)
model=KMeans(n_clusters=64)
model.fit_predict(x)
a=model.cluster_centers_

K-means算法应用:图片压缩

from sklearn.datasets import load_sample_image
china=load_sample_image('china.jpg') import sys
sys.getsizeof(china)
sys.getsizeof(image)
import matplotlib.image as img
img.imsave('E://01.jpg',china)
img.imsave('E://02.JPg',image)

K-means算法应用:图片压缩

K-means算法应用:图片压缩