Shakira.jpg
I am trying to compress the above image but the output that I am getting is an improper image. I think I am doing the PCA steps correctly, but something is going wrong at the final step.
Shakira compressed
import pylab as plt
import numpy as np
img = plt.imread("shakira.jpg")
print(img.shape)
plt.axis('off')
plt.imshow(img)
plt.show()
img_reshaped = np.reshape(img, (930, 1860))
print(img_reshaped.shape)
from sklearn.decomposition import PCA
pca = PCA(.95)
pca.fit(img_reshaped)
img_transformed = pca.transform(img_reshaped)
print(img_transformed.shape)
img_inverse = pca.inverse_transform(img_transformed)
print(img_inverse.shape)
plt.imshow(img_inverse)
plt.show()
img_inverse_reshaped = np.reshape(img_inverse, (930,620,3))
print(img_inverse.shape)
plt.axis('off')
plt.imshow(img_inverse_reshaped)
plt.show()