How to perform image compression using Huffman coding

Key takeaways:

  • Huffman coding is a lossless compression algorithm that assigns shorter codes to frequent pixel values, reducing file size without data loss.

  • Image compression using Huffman coding involves analyzing the image, building a frequency table of pixel values, and creating a Huffman tree based on their frequencies.

  • Huffman codes are generated by assigning binary codes to pixel values based on the tree structure, with lower frequencies getting longer codes.

  • The compressed image is represented as a binary string, reducing the size compared to the original, while maintaining the data.

  • Huffman coding effectively compresses grayscale and color images, making it a valuable tool for efficient storage and transmission.

Image compression is a crucial technique in the modern digital era to reduce the size of image files while preserving quality. Huffman coding is a popular lossless compression method that reduces file size without losing information. In this Answer, we will explain how Huffman coding works and discuss the steps to perform image compression using this technique.

What is Huffman coding?

Huffman coding is a lossless data compression algorithm developed by David A. Huffman in 1952. The key idea behind it is to assign shorter codes to more frequent elements and longer codes to less frequent ones, thus minimizing the average code length and reducing file size.

In image compression, Huffman coding is applied to pixel values or symbols in an image. Creating a frequency-based binary tree ensures that the most frequent pixel values are encoded with the fewest bits, making the overall file size smaller.

Steps to perform image compression using Huffman coding

Here’s a step-by-step guide on how to perform image compression using Huffman coding:

1. Analyze the image

The load_image(image_path) function loads an image from the provided path (image_path) and converts it into a grayscale format. The image is then flattened into a 1D array of pixel values, allowing easier manipulation.

import numpy as np
import matplotlib.pyplot as plt
# Step 1: Analyze the Image
def load_image(image_path):
try:
image = Image.open(image_path).convert('L') # Convert to grayscale
pixels = np.array(image).flatten() # Convert the 2D image into a 1D array of pixel values
return image, pixels
except Exception as e:
print(f"Error: {e}")
return None, None
Code to analyze the image

2. Build a frequency table

The build_frequency_table(pixels) function counts the occurrences of each unique pixel value in the image. A dictionary is created where keys represent pixel values, and values represent the frequency of occurrence.

# Step 2: Build a Frequency Table
def build_frequency_table(pixels):
unique, counts = np.unique(pixels, return_counts=True)
pixel_frequencies = dict(zip(unique, counts))
return pixel_frequencies
Code to build a frequency table

3. Create a Huffman tree

The build_huffman_tree(pixel_frequencies) function builds a Huffman tree using the frequency table. It uses a priority queue (min heap) where nodes with lower frequencies are higher up in the tree, and the left child is assigned the code 0, while the right child is assigned the code 1.

import heapq
# Step 3: Create a Huffman Tree
def build_huffman_tree(pixel_frequencies):
heap = [[weight, [symbol, ""]] for symbol, weight in pixel_frequencies.items()]
heapq.heapify(heap)
while len(heap) > 1:
low = heapq.heappop(heap)
high = heapq.heappop(heap)
for pair in low[1:]:
pair[1] = '0' + pair[1]
for pair in high[1:]:
pair[1] = '1' + pair[1]
heapq.heappush(heap, [low[0] + high[0]] + low[1:] + high[1:])
return heap
Code to create the Huffman tree

4. Generate Huffman codes

The generate_huffman_codes(huffman_tree) function generates the actual Huffman codes for each pixel value. The codes are a binary string where each character corresponds to a pixel value in the original image.

# Step 4: Generate Huffman Codes
def generate_huffman_codes(huffman_tree):
huffman_codes = {}
for pair in huffman_tree[0][1:]:
huffman_codes[pair[0]] = pair[1]
return huffman_codes
Code to generate Huffman codes

5. Compress the image

The compress_image(pixels, huffman_codes) function compresses the image by converting each pixel value into its corresponding Huffman code. The result is a compressed string representation of the image.

# Step 5: Compress Image
def compress_image(pixels, huffman_codes):
compressed_image = ''.join(huffman_codes[pixel] for pixel in pixels)
return compressed_image
Code to encode the image

6. Show original and simulated compressed image

The display_images(original_image) function visualizes the original image and a placeholder for the compressed image. As the actual binary compression cannot be visualized as an image, the original image is displayed as a placeholder to simulate the result.

# Step 6: Show original and "compressed" images
def display_images(original_image):
# Display the original image
plt.figure(figsize=(8, 4))
# Plot original image
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(original_image, cmap='gray')
plt.axis('off')
# Placeholder for compressed image (visualization)
plt.subplot(1, 2, 2)
plt.title("Simulated Compressed Image")
plt.imshow(original_image, cmap='gray')
plt.axis('off')
plt.show()
Code to show original and simulated compressed image

Try it yourself!

Launch the Jupyter Notebook below to see the image compression using Huffman coding:

Please note that the notebook cells have been preconfigured to display the outputs
for your convenience and to facilitate an understanding of the concepts covered. 
This hands-on approach will allow you to experiment with the memory techniques discussed, providing a more immersive learning experience.
Image compressing using Huffman coding

Conclusion

Huffman coding is an efficient lossless compression method that can significantly reduce the size of image files while preserving the original data. By assigning shorter codes to frequent pixel values, it optimizes the encoding process. Although this example demonstrates Huffman coding on a grayscale image, the same technique can be applied to color images or other types of data.

With the ability to compress images effectively, Huffman coding proves to be a valuable tool in digital storage and transmission, allowing you to handle large volumes of visual data efficiently.

Quiz

Test your knowledge about image compression using Huffman coding.

1

In Huffman coding, which symbols are assigned shorter binary codes?

A)

Symbols with the lowest probabilities

B)

Symbols with the highest probabilities

C)

Symbols with the longest names

D)

Symbols with even probabilities

Question 1 of 30 attempted

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do you compress data using Huffman coding?

Huffman coding compresses data by assigning shorter binary codes to more frequent symbols and longer codes to less frequent ones, reducing the overall file size.


Can Huffman coding be used for images?

Yes, Huffman coding can be applied to images by compressing the pixel values, reducing the file size without losing data.


What is Huffman coding procedure in digital image processing?

The procedure involves analyzing pixel frequencies, building a Huffman tree, generating binary codes for pixels, and compressing the image by replacing pixels with corresponding codes.


Does JPEG use Huffman coding?

Yes, JPEG uses Huffman coding as part of its compression process, specifically during the entropy encoding phase.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved