读取示例图片,读取示例图片
from sklearn.datasets import load_sample_image
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
china = load_sample_image('china.jpg')
plt.imshow(china)
plt.show()
观察图片数据格式
image = china[::3,::3]
X = image.reshape(-1,3)
print(china.shape,image.shape,X.shape)
(427, 640, 3) (143, 214, 3) (30602, 3)
考虑如何压缩
image = china[::3,::3]
plt.imshow(image)
plt.show()
image
可以用matplotlib.image.imread读取图片
import matplotlib.image as img
zj = img.imread('F:\timg.jpg')
plt.imshow(zj)
plt.show()
zj
先缩小图片分辨率
zjs = zj[::3,::3]
plt.imshow(zjs)
plt.show()
zjs