In [1]:
import numpy as np
import matplotlib.pyplot as pt
In [2]:
from PIL import Image

with Image.open("andreas.jpeg").resize((500,500)) as img:
    rgb_img = np.array(img)
rgb_img.shape
/usr/lib/python3/dist-packages/PIL/Image.py:1513: ResourceWarning: unclosed file <_io.BufferedReader name='andreas.jpeg'>
  self.load()
Out[2]:
(500, 500, 3)
In [3]:
img = np.sum(rgb_img, axis=-1)
In [4]:
pt.imshow(img, cmap="gray")
Out[4]:
<matplotlib.image.AxesImage at 0x7ffa845dbe48>
In [5]:
u, sigma, vt = np.linalg.svd(img)
sigma

pt.plot(sigma)
Out[5]:
[<matplotlib.lines.Line2D at 0x7ffa844f8198>]
In [11]:
compressed_img = (
    sigma[0] * np.outer(u[:, 0], vt[0])
    )

pt.imshow(compressed_img, cmap="gray")
Out[11]:
<matplotlib.image.AxesImage at 0x7ffa8432feb8>
In [37]: