An interactive image compression web app built using Singular Value Decomposition (SVD) for low-rank matrix approximation.
This project demonstrates how linear algebra techniques reduce storage while preserving image quality.
- Upload any RGB image
- Adjust compression rank using slider
- Real-time compressed image preview
- Channel-wise RGB SVD compression
- Compression ratio calculation
- Reconstruction error (Frobenius norm)
- Rank vs compression analysis graphs
- Download compressed image output
Let an image be represented as a matrix
$$
\mathbf{A} \in \mathbb{R}^{m \times n}
$$
for a grayscale channel (pixel intensities 0–255). An RGB image has three such matrices
Compression seeks a rank‑$k$ matrix
Theorem. Any real matrix
where
-
$\mathbf{U} \in \mathbb{R}^{m \times m}$ is orthogonal:$\mathbf{U}^\top \mathbf{U} = \mathbf{I}_m$ -
$\mathbf{V} \in \mathbb{R}^{n \times n}$ is orthogonal:$\mathbf{V}^\top \mathbf{V} = \mathbf{I}_n$ -
$\boldsymbol{\Sigma} \in \mathbb{R}^{m \times n}$ is diagonal with singular values
Columns of
Keep only the first
The rank‑$k$ approximation is
This is the optimal rank‑$k$ approximation in Frobenius and spectral norms (Eckart–Young–Mirsky).
Original storage (one channel):
Compressed storage:
For RGB (3 channels):
Compression ratio (percentage saved):
Frobenius norm:
Error after compression:
Relative error:
Total energy = sum of squared singular values:
Energy retained by rank‑$k$:
This equals the fraction of variance explained in PCA.
Theorem. For any
The truncated SVD
A colour image is a 3‑tensor
Compress each channel independently:
Reconstruct by stacking:
| Operation | Complexity |
|---|---|
| Full dense SVD | |
| Truncated / randomised SVD (to rank |
For large images, full SVD becomes prohibitive. Scaling requires randomised or iterative methods.
Pixel values are integers in
- Convert to
float64before SVD. - Perform all multiplications in floating point.
- Clip reconstructed values to
$[0,255]$ . - Convert back to
uint8only for final output.
This ensures the computed Frobenius error matches the theoretical tail‑sum of singular values up to machine epsilon.
| Quantity | Formula |
|---|---|
| SVD | |
| Rank‑$k$ approx | |
| Compression ratio | |
| Reconstruction error | |
| Energy retention | |
| Optimality | $|\mathbf{A} - \mathbf{A}_k|F = \min{\mathrm{rank}(\mathbf{B})\le k} |\mathbf{A} - \mathbf{B}|_F$ |
| Full SVD complexity |
This framework rigorously justifies the implementation and provides exact metrics for the storage–fidelity trade‑off.
The application computes:
- Compression Ratio (% storage saved)
- Reconstruction Error (Frobenius Norm)
Frobenius norm:
||A − Aₖ||
Lower error indicates better reconstruction quality.
- Python
- NumPy
- Streamlit
- Pillow
- Matplotlib
SVD-Image-Compression-Tool/ │ ├── app.py ├── svd_utils.py ├── requirements.txt ├── sample_images/ └── README.md
Clone repository:
git clone https://github.com/BitwiseSage/SVD-Image-Compression-Tool.git
Navigate into folder:
cd SVD-Image-Compression-Tool
Install dependencies:
pip install -r requirements.txt
Run application:
python -m streamlit run app.py
- Image storage optimization
- Facial recognition preprocessing
- Recommender systems
- Signal processing
- Dimensionality reduction (PCA)
Example compression output:
pdf file of demo placed in sample_images folder as "demo.pdf"
Developed as part of a Linear Algebra course project demonstrating practical applications of Singular Value Decomposition (SVD) in image compression and matrix approximation.