Project

Compress a photo with SVD

You build a notebook that finds a photo's strongest pieces by hand and holds them against NumPy's SVD, keeps the photo in a share of its numbers and measures what was lost, and does the same to a colour photo of your own.

About 2.5 hours. You need your own computer, with uv, or Colab, which needs a Google account; a photo of your own, as a JPEG or a PNG.

You build it in the starter, one step at a time. The finished project is there to read when you are stuck.

Get the starterRead the finished project

  1. Part 1

    The photo as numbers

    A colour photo is a grid of pixels, and each pixel is three lights: red, green and blue. Here you turn it into one matrix of greys, the matrix the rest of the project works on.

    UsesVectorsAdding and scaling vectorsThe dot productMatrix multiplication

    1. The photoRun it
    2. One pixel's greyYour turn
    3. Every pixel at onceYour turn
    4. Products and no sumsRun it

    Open this part in Colab

    When you have run it

    What is the grey of the pixel at row 100, column 500, to two decimal places?

  2. Part 2

    The strongest piece, by hand

    The lesson on eigenvectors showed how a computer finds the strongest line: move any arrow again and again, and see where it points. Here you do that to the grey photo, and find its strongest piece without NumPy's SVD. Then you hold your answer against NumPy's.

    UsesA matrix is a transformationEigenvectors and eigenvaluesDistance and cosine similarityProjectionRank and low-rank matricesSVD and low-rank approximation

    1. A matrix that moves arrowsRun it
    2. One moveYour turn
    3. Again and againYour turn
    4. Against NumPyRun it
    5. The strongest pieceYour turn
    6. The second pieceOn your own

    Open this part in Colab

    When you have run it

    What is the photo's strongest singular value, to the nearest whole number?

  3. Part 3

    Keep the strongest, and measure the gap

    Now NumPy's SVD does the splitting. You keep the strongest pieces, count what they cost and measure how far the result is from the photo. Then you find out why a photo can be kept in so few numbers, when the same greys in random places cannot.

    UsesSVD and low-rank approximationMatrix multiplicationDistance and cosine similarityProjectionHigh-dimensional space

    1. Keep k piecesYour turn
    2. What it costs, and how far off it isYour turn
    3. A row of photosRun it
    4. The gap without rebuildingYour turn
    5. The same greys, in random placesOn your own

    Open this part in Colab

    When you have run it

    How many pieces does the shuffled photo need to stay within a tenth?

  4. Part 4

    Your own photo, in colour

    A colour photo is three matrices, one for each light. Keep each one's strongest pieces, put the three back together, and you have the photo in a share of its numbers. You try it on the course's photo first, then on one of your own.

    UsesVectorsAdding and scaling vectorsSVD and low-rank approximationDistance and cosine similarity

    1. Three lights, three matricesYour turn
    2. A fifth of its numbersRun it
    3. Your photoOn your own
    4. Where it lost the mostOn your own

    Open this part in Colab

    When you have run it

    Kept in a fifth of its numbers, what is the gap of the course's colour photo, to two decimal places?

Run on Python 3.13, NumPy 2.5.5, scikit-learn 1.9.1, matplotlib 3.11.2, Pillow 13.0.0. Checked on .