Skip to content

K-Means Image Compression is a Python-based project that compresses an image by reducing the number of colors used. This technique is implemented using the K-Means clustering algorithm, making it ideal for those looking to understand and apply machine learning concepts in image processing.

License

Notifications You must be signed in to change notification settings

pragyapranati/K-Means-Image-Compression

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

🎨 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.

About

K-Means Image Compression is a Python-based project that compresses an image by reducing the number of colors used. This technique is implemented using the K-Means clustering algorithm, making it ideal for those looking to understand and apply machine learning concepts in image processing.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages