🌍 Become a marimo ambassador!

Apply
16 / 103
Low-Rank Approximation

Low-Rank Approximation

by marimo team

Compress images in real time using a low-rank approximation via the SVD.

About this notebook

This notebook computes a low-rank approximation of a photo from your webcam. It captures the photo with the WebcamCapture widget from wigglystuff, converts it to a NumPy array, and compresses it with a truncated singular value decomposition. A matplotlib figure shows the original photo and the approximation side by side.

Two widgets drive the notebook. The camera widget takes the photo, and a slider sets the rank of the approximation, from 1 to 128. When you move the slider, marimo re-runs the cells that depend on it and redraws the comparison figure. Until you take a photo, marimo stops the dependent cells and shows a prompt instead.

The core function applies numpy.linalg.svd to each of the three RGB channels and reconstructs each channel from the first k singular values. A higher rank keeps more detail, and a lower rank shows how few components the image needs to stay recognizable.

Related notebooks