Skip to content

Instantly share code, notes, and snippets.

@Eliran-Turgeman
Last active September 16, 2021 13:39
Show Gist options
  • Save Eliran-Turgeman/5278077e675202ced04efc62ed6a110a to your computer and use it in GitHub Desktop.
Save Eliran-Turgeman/5278077e675202ced04efc62ed6a110a to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
MAX_RANK = 100
FNAME = 'bcollie.jpg'
image = Image.open(FNAME).convert("L")
img_mat = np.asarray(image)
U, s, V = np.linalg.svd(img_mat, full_matrices=True)
s = np.diag(s)
for k in range(MAX_RANK + 1):
approx = U[:, :k] @ s[0:k, :k] @ V[:k, :]
img = plt.imshow(approx, cmap='gray')
plt.title(f'SVD approximation with degree of {k}')
plt.plot()
pause_length = 0.0001 if k < MAX_RANK else 5
plt.pause(pause_length)
plt.clf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment