Image Compression is a modern Flask web app that reduces image size using K-Means color quantization.
Instead of storing millions of unique colors, the algorithm intelligently groups similar colors into clusters β dramatically shrinking file size while preserving visual quality.
π¦ From a simple Jupyter Notebook experiment, this project has been upgraded into a fully interactive web application with:
- β Upload
- β Compress
- β Compare
- β Download
- β View metrics
All in seconds.
Upload Image
β
K-Means Clustering (Color Quantization)
β
Rebuild using K colors
β
Show Preview + Compression Stats
β
Download Optimized Image
- K-Means color compression
- Adjustable cluster count (8 β 64 colors)
- RGB β cluster β reconstruct pipeline
- Automatic JPEG optimization
- Unique original colors count
- Original file size
- Compressed file size
- Compression percentage
- Side-by-side comparison
- Glassmorphism design
- Gradient theme
- Smooth animations
- Loading state
- Mobile responsive
- Bootstrap powered
- Flask routing
- Static file handling
- Secure uploads
- Fast NumPy processing
Image β reshape to:
(height Γ width, 3)
Each pixel becomes:
[R, G, B]
Group pixels into K clusters
Example:
16 million colors β reduced palette ( 8, 16, 32, 64 )
Replace each pixel with its cluster center.
Result:
Smaller memory + Similar visual quality
| Layer | Tech |
|---|---|
| Backend | Flask |
| ML Algorithm | Scikit-Learn KMeans |
| Image Processing | OpenCV |
| Math | NumPy |
| Frontend | HTML + Bootstrap + CSS |
| Icons | FontAwesome |
image-compression/
β
βββ app.py
βββ requirements.txt
β
βββ static/
β βββ uploads/
β βββ outputs/
β
βββ templates/
β βββ index.html
β
βββ README.md
git clone https://github.com/SACHIN-S-2004/Image-Compression.git
cd Image-Compressionpip install -r requirements.txtpython app.pyhttp://127.0.0.1:5000
| Metric | Value |
|---|---|
| Original Colors | 48,231 |
| After K=16 | 16 |
| Size Before | 820 KB |
| Size After | 210 KB |
| Saved | 74% |
- Drag & drop upload
- Multiple images batch compression
- Slider for real-time K selection
- React frontend version
- Auto cluster suggestion
This project demonstrates:
- β Unsupervised Learning (K-Means)
- β Image processing fundamentals
- β Flask backend development
- β Practical ML deployment
Give it a star β it helps a lot!
.png)
.png)