Skip to content

A lossless image compression and decompression algorithm utilizing the Huffman coding technique to efficiently reduce image file sizes while maintaining image quality.

Notifications You must be signed in to change notification settings

Roodaki/Huffman-Image-Compressor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Image Compression/Decompression using Huffman-Coding

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.

Table of Contents

Introduction

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 Principles

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.

Huffman Coding

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.

Project Structure

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.

Requirements

  • Python 3.X

Usage Gudie

To compress and decompress an image using Huffman coding, follow these steps:

  1. Clone the repository to your local machine: git clone https://github.com/Roodaki/Huffman-Image-Compressor
  2. Run the program using Python: python main.py
  3. Enter the path of the image you want to compress and decompress.
  4. 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.
  5. The compression ratio (CR) will be printed to the termianl.

About

A lossless image compression and decompression algorithm utilizing the Huffman coding technique to efficiently reduce image file sizes while maintaining image quality.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages