import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
img = Image.open('Images/mosque.jpg')
plt.figure(figsize=[18,12])
plt.imshow(img)
plt.axis('off')
plt.show()
print(type(img))
img_array = np.array(img)
print(img_array.shape)
X = img_array.reshape(1024 * 678, 3) / 255
print(X.shape)
from sklearn.cluster import MiniBatchKMeans
kmeans = MiniBatchKMeans(n_clusters=32)
kmeans.fit(X)
pred_clusters = kmeans.predict(X)
X_rc = kmeans.cluster_centers_[pred_clusters]
img_array_rc = X_rc.reshape(1024, 678, 3) * 255
img_array_rc = img_array_rc.astype('uint8')
image_rc = Image.fromarray(img_array_rc, 'RGB')
plt.figure(figsize=[18,12])
plt.imshow(img_array_rc)
plt.axis('off')
plt.show()
image_rc.save('images/mosque_rc.jpg')