A Python-based implementation of the Huffman Coding algorithm for efficient, lossless compression and decompression of color images. This project demonstrates how frequency-based encoding can reduce file size without losing a single pixel of quality.
- True Lossless Compression: Recovers the original image pixel-for-pixel.
- Color Support: Handles RGB color tuples using custom Huffman Tree logic.
- Modular Architecture: Separated logic for the algorithm, file handling, and main execution.
- Real-time Statistics: Calculates original vs. compressed size and saving percentage.
- Accuracy Verification: Built-in check to verify decompressed data integrity.
main.py: The entry point that orchestrates the compression and decompression flow.huffman_logic.py: Contains the core algorithm, Node class, and tree traversal logic.file_handler.py: Manages bit-packing, padding, and binary file I/O operations.test_image.jpg: Sample input image for testing.
- Frequency Analysis: The script reads the image and counts the occurrences of each unique (R, G, B) pixel tuple.
- Building the Tree: A Min-Heap is used to build a Huffman Tree where frequent colors get shorter binary codes.
- Encoding: Every pixel is replaced by its corresponding Huffman bit-string.
- Bit-Packing: Since bits cannot be saved directly, the bits are padded and packed into 8-bit bytes for storage in a
.binfile. - Decompression: The binary file is read, padding is removed, and the Huffman Tree is traversed to reconstruct the original pixels.
- Python 3.x
- OpenCV & NumPy
pip install opencv-python numpy
git clone [https://github.com/true-brace05/huffman_coding.git](https://github.com/true-brace05/huffman_coding.git)
cd huffman_coding
python3 main.py