import numpy as np
import matplotlib.pyplot as plt
import kmeans

KCENTROIDS = 16
MAX_ITERS = 200
EPSILON = 1e-7

"""
Explanation: Homework 3 Problem 5
End of explanation
"""


Ilarge = plt.imread('mandrill-large.tiff')
Ilarge = np.uint8(np.round(Ilarge))

plt.imshow(Ilarge)
plt.show()

"""
Explanation: Part 5.a.
Here we load the large mandrill image and display it.
End of explanation
"""


Ismall = plt.imread('mandrill-small.tiff')
Ismall = np.uint8(np.round(Ismall))

plt.imshow(Ismall)
plt.show()

"""
Explanation: Part 5.b.
Here we load the small mandrill image and display it.
End of explanation
"""


clusters_small, centroids_small = kmeans.kmeans(Ismall, kcentroids=KCENTROIDS, epsilon=EPSILON, max_iterations=200)

"""
Explanation: Now apply the K-Means algorithm with k centroids to the small image.
End of explanation
"""


plt.imshow(np.array([centroids_small]))
plt.show()

new_image_small = kmeans.make_image(clusters_small, centroids_small)

plt.imshow(new_image_small)
plt.show()

"""
Explanation: The color palette obtained by running K-Means is the following.
End of explanation
"""


clusters_large, centroids_large = kmeans.kmeans(Ilarge, kcentroids=KCENTROIDS, epsilon=EPSILON, max_iterations=MAX_ITERS)

new_image_large = kmeans.make_image(clusters_large, centroids_large)

plt.imshow(new_image_large)
plt.show()

"""
Explanation: Part 5.c.
Do the same as above for the large mandrill image.
End of explanation
"""


