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!

