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

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

pt.plot(sigma)
Out[15]:
[<matplotlib.lines.Line2D at 0x7f17dd500080>]
In [24]:
compressed_img = (
    sigma[0] * np.outer(u[:, 0], vt[0])
    + sigma[1] * np.outer(u[:, 1], vt[1])
    + sigma[2] * np.outer(u[:, 2], vt[2])
    + sigma[3] * np.outer(u[:, 3], vt[3])
    + sigma[4] * np.outer(u[:, 4], vt[4])
    + sigma[5] * np.outer(u[:, 5], vt[5])
    )

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