Skip to content

Latest commit

 

History

History
82 lines (53 loc) · 2.62 KB

README.md

File metadata and controls

82 lines (53 loc) · 2.62 KB

🎨 K-Means Image Compression

✨ Features

  • 🖼️ 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.

🚀 How to Run

Prerequisites

  • 🐍 Python 3.x
  • 📦 Required libraries: numpy, matplotlib, PIL, scikit-learn

Installation

  1. Clone the repository:

    git clone https://github.com/pragyapranatie/k-means-image-compression.git
    cd k-means-image-compression
  2. Install the required libraries:

    pip install numpy matplotlib pillow scikit-learn

Usage

  1. Place your image file in the repository directory. Update the image_file variable in the script with your image filename.

  2. Run the script:

    python kmeans_image_compression.py
  3. Follow the prompts to enter the desired number of colors for image compression.

🛠️ Tech Stack

  • Python
  • Numpy
  • Matplotlib
  • Pillow (PIL)
  • Scikit-learn

🧠 Logic Explanation

📚 K-Means Clustering

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.

🔄 Steps Involved

  1. Initialization: Select K initial centroids randomly or using the K-Means++ algorithm.
  2. Assignment: Assign each data point to the closest centroid.
  3. Update: Calculate the new centroids as the mean of all points assigned to each cluster.
  4. 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.

🌟 Visual Examples

Original Image with 13930 unique colors

image

image

Compressed Image with 20 Colors

image image

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request or open an Issue for any improvements or bug fixes.

📜 License

This project is licensed under the Apache License.