This project takes any input image and reduces its color palette to k representative colors using K-Means clustering. It then recolors the entire image so that every pixel is replaced by the nearest cluster color, producing a simplified, stylized version of the original.
- 🧠 Uses K-Means clustering to find dominant colors in an image.
- 🎨 Reconstructs the image using only those k colors.
- 📊 Supports adjustable
k(number of colors). - 💾 Saves both the quantized image.
- ⚡ Works with most common image formats (PNG, JPG, etc.).
- Python 3.x
- NumPy — fast numerical computation
- imageio.v3 — image processing
Clone the repository and install dependencies:
git clone https://github.com/yourusername/image-color-quantizer.git
cd image-color-quantizer
pip install -r requirements.txtBasic command-line usage:
python main.py --input input.jpg --k 8 --output output.jpg| Flag | Description | Default |
|---|---|---|
--input |
Path to the input image | required |
--output |
Path to save the output image | quantized.png |
--k |
Number of clusters (colors) | 3 |
| Original | Quantized (k=3) | Quantized (k=6) |
|---|---|---|
- Load the image and reshape its pixels into a 2D array of RGB values.
- Run K-Means clustering to group pixels into k clusters.
- Replace each pixel’s color with the centroid of its assigned cluster.
- Reshape and save the new quantized image.
- Image compression and simplification
- Artistic effects (posterization, stylization)
- Color palette extraction
- Data visualization or color analysis
- Add GPU acceleration with CuML or PyTorch
This project is licensed under the MIT License.