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.
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.
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.
Here’s a step-by-step guide on how to perform image compression using Huffman coding:
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 npimport matplotlib.pyplot as plt# Step 1: Analyze the Imagedef load_image(image_path):try:image = Image.open(image_path).convert('L') # Convert to grayscalepixels = np.array(image).flatten() # Convert the 2D image into a 1D array of pixel valuesreturn image, pixelsexcept Exception as e:print(f"Error: {e}")return None, None
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 Tabledef build_frequency_table(pixels):unique, counts = np.unique(pixels, return_counts=True)pixel_frequencies = dict(zip(unique, counts))return pixel_frequencies
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 Treedef 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
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 Codesdef generate_huffman_codes(huffman_tree):huffman_codes = {}for pair in huffman_tree[0][1:]:huffman_codes[pair[0]] = pair[1]return huffman_codes
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 Imagedef compress_image(pixels, huffman_codes):compressed_image = ''.join(huffman_codes[pixel] for pixel in pixels)return 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" imagesdef display_images(original_image):# Display the original imageplt.figure(figsize=(8, 4))# Plot original imageplt.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()
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.
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.
Test your knowledge about image compression using Huffman coding.
In Huffman coding, which symbols are assigned shorter binary codes?
Symbols with the lowest probabilities
Symbols with the highest probabilities
Symbols with the longest names
Symbols with even probabilities
Haven’t found what you were looking for? Contact Us
Free Resources