Skip to content

TomasBivainis/image-k-clustering

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎨 Image Color Quantizer (K-Means Color Reduction)

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.


🚀 Features

  • 🧠 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.).

🧰 Tech Stack

  • Python 3.x
  • NumPy — fast numerical computation
  • imageio.v3 — image processing

📦 Installation

Clone the repository and install dependencies:

git clone https://github.com/yourusername/image-color-quantizer.git
cd image-color-quantizer
pip install -r requirements.txt

🖼️ Usage

Basic command-line usage:

python main.py --input input.jpg --k 8 --output output.jpg

Arguments

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

🔍 Example

Original Quantized (k=3) Quantized (k=6)

🧪 How It Works

  1. Load the image and reshape its pixels into a 2D array of RGB values.
  2. Run K-Means clustering to group pixels into k clusters.
  3. Replace each pixel’s color with the centroid of its assigned cluster.
  4. Reshape and save the new quantized image.

🎯 Applications

  • Image compression and simplification
  • Artistic effects (posterization, stylization)
  • Color palette extraction
  • Data visualization or color analysis

🛠️ Future Improvements

  • Add GPU acceleration with CuML or PyTorch

📝 License

This project is licensed under the MIT License.


About

Python tool that performs k-means clustering on input images, reducing them to k dominant colours while minimizing the sum of squared errors.

Resources

License

Stars

Watchers

Forks

Contributors

Languages