import matplotlib.pyplot as plt
import numpy as np
img = plt.imread("imagedata.jpg")
plt.axis('off')
plt.imshow(img)
plt.show()
print(img.shape)
print(img)
#The array has 564 rows each of pixel 400x3.
#Reshape it into the form of a matrix that PCA can understand. # 1200 = 400 * 3
img_reshaped = np.reshape(img, (564, 1200))
print(img_reshaped.shape)
print(img_reshaped)
img_mean = img_reshaped.mean(axis=0)
img_reshaped = img_reshaped-img_mean
from sklearn.decomposition import PCA
components=40
pca = PCA(n_components=components)
pca.fit(img_reshaped)
#Following represents the values in the new coordinate system T=XW
img_transformed_coordinate = pca.transform(img_reshaped)
print(img_transformed_coordinate.shape)
#To go back to the old coordinate systesm, either use the inbuilt command or do the operations manually X=TW'
#img_original_coordinate = pca.inverse_transform(img_transformed_coordinate)
img_original_coordinate = img_transformed_coordinate.dot(pca.components_)
#Shifting the mean to original values
img_original_coordinate = img_original_coordinate+img_mean
#Reshaping the matrix to 564*400*3
img_final = np.reshape(img_original_coordinate, (564,400,3))
img_final = img_final.astype('int')
img_final[img_final<0] = 0
img_final[img_final>255] = 255
plt.axis('off')
plt.imshow(img_final)
plt.savefig('imagedata_new.jpg')
plt.show()
#Number of values required to store the original image
original_number_of_values = 564*400*3
#Number of values required to store the new image
#Values in transformed coordinate + principal component values + mean values
new_number_of_values = 564*components+1200*components+components
space_required_in_percentage = new_number_of_values/original_number_of_values*100
print("%.2f" % space_required_in_percentage)