from
sklearn.datasets
import
load_sample_image
import
matplotlib.pyplot as plt
from
sklearn.cluster
import
KMeans
import
numpy as np
flower
=
load_sample_image(
'flower.jpg'
)
plt.imshow(flower)
plt.show()
image
=
flower[::
3
,::
3
]
plt.imshow(image)
plt.show()
x
=
image.reshape(
-
1
,
3
)
n_colors
=
64
model
=
KMeans(n_colors)
labels
=
model.fit_predict(x)
colors
=
model.cluster_centers_
new_image
=
colors[labels]
new_image
=
new_image.reshape(image.shape)
plt.imshow(new_image.astype(np.uint8))
plt.show()
import sys
print(sys.getsizeof(flower))
print(sys.getsizeof(new_image))