1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
from sklearn.datasets import load_sample_image import matplotlib.image as img from sklearn.cluster import KMeans import numpy as np from matplotlib import pyplot as plt
picture = load_sample_image('china.jpg') pic2 = img.imread('v.jpg')
image = picture[::3,::3] plt.imshow(image) img.imsave('pure.jpg',image) plt.show()
X = image.reshape(-1,3) mod = KMeans(n_clusters = 64) labels = mod.fit_predict(X) colors = mod.cluster_centers_
new_img = colors[labels] new_img = new_img.reshape(image.shape) new_img = new_img.astype(np.uint8) print(new_img)
plt.imshow(new_img) img.imsave('E://zip.jpg',new_img) plt.show()
|