Compressing a photo with PCA

Explainer • October 24, 2026 • Raymond Lee

An interactive demo that treats each row of a grayscale photo as a vector, keeps the first k principal components, and rebuilds the picture from them.

Each row of a grayscale image is a vector of pixel brightnesses. Principal component analysis (PCA) finds the few directions along which those rows vary most. Keep only the first k directions, describe each row by k numbers, and you can rebuild a recognisable picture from a fraction of the original data.

Drag the slider below to choose how many components to keep. Everything runs in your browser, including photos you load yourself.

Original
Rebuilt from k components
10principal components kept, out of 256
–of the variance kept
–numbers stored
–of the original size
–PSNR (higher is closer)

Where the variance lives

Cumulative share of row-to-row variance captured by the first k components. The curve rises steeply, which is why a handful of components already gives a recognisable picture.

The first eight layers

Each tile is one component’s contribution: its row pattern weighted by every row’s score. Mid-grey is zero, light adds brightness, dark removes it. Stacking them on the mean image rebuilds the photo.

How it works

Treat the image as a matrix X with H rows and W columns. Subtract each column’s mean, then form the W×W covariance matrix C = XᵀX / (H − 1).

The eigenvectors of C, sorted by eigenvalue, are the principal components. Each one is a row-shaped pattern, orthogonal to all the others, and its eigenvalue says how much of the variance between rows that pattern explains. The demo computes them with a Householder tridiagonalisation followed by implicit QL iteration, the same approach used in EISPACK and JAMA, written in plain JavaScript.

Projecting every row onto the first k components gives an H×k table of scores. The compressed image consists of those scores, the k component vectors, and the column means: Hk + Wk + W numbers instead of HW. For the 256×256 sample scene, ten components need 5,376 numbers, about 8% of the original 65,536.

To decompress, multiply the scores by the components and add the mean back. Structure that runs along rows, such as the horizon and the lake’s edge, survives with very few components. Fine texture and diagonal edges, like the jetty, need many more, which the error view makes easy to see.

Why this is a teaching example

Real image codecs do not work this way. JPEG uses a fixed cosine basis on 8×8 blocks, so it never has to store a basis per image. PCA learns a basis tailored to one picture and then has to ship it alongside the scores, which is why the storage count climbs past 100% once k approaches half the image width.

The same idea carries much further when the basis is shared across many examples. Low-rank approximations of this kind sit underneath latent semantic analysis, recommender-system factorisations, and LoRA adapters for language models, where a weight update is stored as the product of two thin matrices.

Photos you load are resized so that the longer side is 256 pixels and converted to grayscale. They never leave your browser.

More from Insights

Reliable Inference

Evaluator aware decoding

July 5, 2026

Evaluator aware decoding: let the model write the expression, let the engine write the answer