This project demonstrates an image compression technique using the K-Means clustering algorithm. The original image is compressed by reducing the number of unique colors, which can significantly decrease the image file size while maintaining visual quality.
Image compression is a process of reducing the file size of an image while preserving its quality. This project utilizes the K-Means clustering algorithm to achieve compression by clustering similar colors and reducing the number of unique colors in the image.
-
Clone the repository:
git clone https://github.com/CGbhakt420/Image-Compression.git cd Image-Compression
-
Install the required dependencies:
pip install numpy matplotlib
-
Place the image you want to compress in the project directory and update the
image_path
variable in the script. -
Run the script:
python compress_image.py
find_closest_centroids(X, centroids)
: Finds the closest centroids for each data point in X.compute_centroids(X, idx, K)
: Computes new centroids based on the current cluster assignments.kMeans_init_centroids(X, K)
: Initializes centroids by randomly selecting K data points from X.run_kMeans(X, initial_centroids, max_iters=10, plot_progress=False)
: Runs the K-Means algorithm for a given number of iterations.
- Load and normalize the image.
- Reshape the image data for clustering.
- Initialize and run the K-Means algorithm.
- Reconstruct the compressed image from the cluster centroids.
- Display and compare the original and compressed images.
The compressed image uses fewer colors but retains a high level of visual fidelity compared to the original image. Here is an example of the results:
Contributions are welcome! Please fork the repository and create a pull request with your changes.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request