- 🖼️ Compress images by reducing the number of unique colors.
- 📊 Visualize the original and compressed images.
- 🛠️ Flexible input for the desired number of colors in the compressed image.
- ⚡ Efficient K-Means clustering implementation with customizable iterations.
- 🐍 Python 3.x
- 📦 Required libraries:
numpy
,matplotlib
,PIL
,scikit-learn
-
Clone the repository:
git clone https://github.com/pragyapranatie/k-means-image-compression.git cd k-means-image-compression
-
Install the required libraries:
pip install numpy matplotlib pillow scikit-learn
-
Place your image file in the repository directory. Update the
image_file
variable in the script with your image filename. -
Run the script:
python kmeans_image_compression.py
-
Follow the prompts to enter the desired number of colors for image compression.
- Python
- Numpy
- Matplotlib
- Pillow (PIL)
- Scikit-learn
K-Means clustering is a popular unsupervised machine learning algorithm used for clustering data points. The algorithm aims to partition the data into K clusters, where each data point belongs to the cluster with the nearest mean.
- Initialization: Select K initial centroids randomly or using the K-Means++ algorithm.
- Assignment: Assign each data point to the closest centroid.
- Update: Calculate the new centroids as the mean of all points assigned to each cluster.
- Repeat: Repeat the assignment and update steps until the centroids do not change significantly or a maximum number of iterations is reached.
In this project, K-Means clustering is applied to the RGB values of the image pixels to reduce the number of unique colors, effectively compressing the image.
Contributions are welcome! Please feel free to submit a Pull Request or open an Issue for any improvements or bug fixes.
This project is licensed under the Apache License.