In [1]:

```
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import time
from PIL import Image
```

In [2]:

```
img = Image.open('input/img_6847.jpg')
imggray = img.convert('LA')
plt.figure(figsize=(9, 6))
plt.imshow(imggray);
```

In [3]:

```
imgmat = np.array(list(imggray.getdata(band=0)), float)
imgmat.shape = (imggray.size[1], imggray.size[0])
imgmat = np.matrix(imgmat)
plt.figure(figsize=(9,6))
plt.imshow(imgmat, cmap='gray');
```

Now to compute the singular value decomposition:

In [4]:

```
U, sigma, V = np.linalg.svd(imgmat)
```

In [5]:

```
reconstimg = np.matrix(U[:, :1]) * np.diag(sigma[:1]) * np.matrix(V[:1, :])
plt.imshow(reconstimg, cmap='gray');
```

Even with just the second and third vectors, the shape of the arch begins to appear.

In [6]:

```
for i in xrange(2, 4):
reconstimg = np.matrix(U[:, :i]) * np.diag(sigma[:i]) * np.matrix(V[:i, :])
plt.imshow(reconstimg, cmap='gray')
title = "n = %s" % i
plt.title(title)
plt.show()
```

In [7]:

```
for i in xrange(5, 51, 5):
reconstimg = np.matrix(U[:, :i]) * np.diag(sigma[:i]) * np.matrix(V[:i, :])
plt.imshow(reconstimg, cmap='gray')
title = "n = %s" % i
plt.title(title)
plt.show()
```