This project implements a lossless method of image compression and decompression using the Huffman coding technique. The aim is to reduce the redundancy of image data to store or transmit data in an efficient form. The project provides an easy-to-implement technique and utilizes less memory.
Compression is the process of reducing the quantity of data used to represent a file, image, or video content without excessively reducing the quality of the original data. Image compression is the application of data compression on digital images, where the objective is to reduce the redundancy of the image data. This project proposes a lossless method of image compression and decompression using the Huffman coding technique.
Compression can be divided into two categories: Lossy Compression and Lossless Compression. Lossy compression means that some data is lost when it is decompressed, while lossless compression ensures a bit-for-bit perfect match with the original data.
In an image, there are three types of redundancies that can be compressed to reduce the file size, namely, coding redundancy, interpixel redundancy, and psycho-visual redundancy. Huffman coding is a classical data compression technique that takes advantage of coding redundancy. It uses fewer bits to encode data into binary codes that occur more frequently.
The Huffman coding technique is used in this project to compress and decompress images. It generates an optimal prefix code from a set of probabilities and has been used in various compression applications. The code lengths of Huffman codes are variable, using an integral number of bits. This reduces the average code length, resulting in a smaller size of compressed data than the original.
The project has the following folder structure:
│
├── src/
│ ├── main.py
│ └── huffman_coding.py
│ └── file_handling.py
│
└── IO/
│ ├── Inputs/
│ └── Outputs/
The "src" folder contains the following three Python files:
- main.py: This file is the entry point of the program. It takes the path of the input image and calls functions defined in the other files to compress and decompress the image using Huffman coding.
- huffman_coding.py: This file contains functions to implement Huffman coding, including calculating the frequency of each byte in the input image, building the Huffman tree, generating Huffman codes, and mapping bytes to their corresponding codes.
- file_handling.py: This file contains functions to read and write image files, including reading the input image file as a string of bits and writing the compressed or decompressed image to an output file.
The "IO" folder contains two sub-folders:
- Inputs: This folder contains the input image(s) to be compressed and decompressed.
- Outputs: This folder will contain the compressed and decompressed images generated by the algorithm.
- Python 3.X
To compress and decompress an image using Huffman coding, follow these steps:
- Clone the repository to your local machine:
git clone https://github.com/Roodaki/Huffman-Image-Compressor
- Run the program using Python:
python main.py
- Enter the path of the image you want to compress and decompress.
- The program will automatically compress and then decompress the image using Huffman Coding, and save the output in the "Outputs" folder inside the "IO" directory. Additionally, the program will also generate a file called "huffman_codes.txt" in the "Outputs" folder, which contains the Huffman codes used for compression.
- The compression ratio (CR) will be printed to the termianl.