This project implements image compression using Huffman coding, demonstrating the internal workings of image compression algorithms. It provides a comprehensive visualization of the compression process, including the Huffman tree distribution and compression metrics.
- Image compression using Huffman coding algorithm
- Visualization of compression process and results
- Support for various image formats (PNG, JPG, JPEG)
- Detailed compression metrics and statistics
- Interactive visualization of Huffman code distribution
- Save and load compressed image data
- Batch processing of multiple images
- Python 3.7+
- NumPy
- Pillow (PIL)
- Matplotlib
- tqdm
- Streamlit
- Graphviz
We recommend using uv for dependency management as it's significantly faster than pip. Here's how to set up:
-
First, install
uvif you haven't already:curl -LsSf https://astral.sh/uv/install.sh | sh -
Create and activate a virtual environment:
uv venv source .venv/bin/activate # On Unix/macOS # or .venv\Scripts\activate # On Windows
-
Install dependencies using uv:
uv pip install -r requirements.txt
Alternatively, you can use:
uv sync
.
├── image_compressor.py # Main implementation of the compression algorithm
├── app.py # Streamlit web application
├── demo.py # Demo script to showcase compression
├── requirements.txt # Project dependencies
├── samples/ # Directory for test images
└── README.md # Project documentation
-
Set up the environment as described in the Requirements section.
-
Place your test images in the
samplesdirectory or use the streamlit app to upload images. -
Run the Streamlit app:
streamlit run app.py
Or run the demo script:
python demo.py
The application will:
- Process uploaded images (or images in samples directory for demo)
- Show the original and compressed images
- Display compression metrics
- Visualize the Huffman tree and code distribution
- Save the compressed and decompressed versions
-
Image Loading:
- Images are loaded and converted to grayscale
- Pixel values are extracted as a numpy array
-
Huffman Coding:
- Pixel frequencies are calculated
- Huffman tree is built based on pixel frequencies
- Huffman codes are generated for each pixel value
-
Compression:
- Pixels are encoded using the generated Huffman codes
- Compressed data is stored efficiently
- Compression metrics are calculated
-
Visualization:
- Original vs. compressed image comparison
- Huffman code distribution visualization
- Compression metrics display
The following metrics are calculated and displayed:
- Compression ratio
- Original file size
- Compressed file size
- Percentage of space saved
When you run the application, you'll see:
- Original image alongside the compressed version
- Histogram showing the distribution of Huffman codes
- Interactive Huffman tree visualization
- Detailed compression metrics
Feel free to contribute to this project by:
- Adding new features
- Improving the compression algorithm
- Enhancing visualizations
- Fixing bugs
- Improving documentation
This project is licensed under the MIT License - see the LICENSE file for details.