0% found this document useful (0 votes)
9 views6 pages

Multimedia Rohit Josh i

This lab report details the implementation of the LZW compression algorithm using Python, highlighting its efficiency in lossless data compression. The report covers the algorithm's principles, methods, and provides Python code for both compression and decompression functions. Results indicate successful compression and restoration of original data, affirming the effectiveness of LZW for text and binary data.

Uploaded by

Rohit Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Multimedia Rohit Josh i

This lab report details the implementation of the LZW compression algorithm using Python, highlighting its efficiency in lossless data compression. The report covers the algorithm's principles, methods, and provides Python code for both compression and decompression functions. Results indicate successful compression and restoration of original data, affirming the effectiveness of LZW for text and binary data.

Uploaded by

Rohit Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PULCHOWK CAMPUS

INSTITUTE OF ENGINEERING
TRIBHUVAN UNIVERSITY

Lab Report on:


Compression using LZW Algorithm
And Animation

By
Rohit Joshi (077BEI037)

To
Department of Electronics and Computer Engineering
Pulchowk Campus, Lalitpur
INTRODUCTION:
Data compression is crucial in reducing storage requirements and enhancing data
transmission speeds. Compression techniques are classified into two categories:
lossy and lossless. Both aim to identify and eliminate redundant data, but they differ
in approach and application.
●​ Lossless Compression preserves all original data by identifying statistical
redundancy, ensuring that no information is lost. Examples include
Run-Length Encoding (RLE), Huffman Coding, and Lempel-Ziv-Welch (LZW).
●​ Lossy Compression reduces file size by discarding less significant information,
making it ideal for multimedia files like MP3 and JPEG. Techniques include
Discrete Cosine Transform (DCT) and Vector Quantization.

LZW is a widely used lossless compression algorithm, commonly employed in GIF,


TIFF, and Unix ‘compress’ commands. It dynamically builds a dictionary for encoding
sequences, making it simple to implement and highly efficient in compressing
repetitive data.

OBJECTIVES:
●​ To understand the LZW compression algorithm.
●​ To implement a Python-based LZW compression and decompression system.

LITERATURE REVIEW:
The LZW algorithm, developed by Terry Welch in 1984 as an improvement of LZ78,
has been integral in compression technologies. Unlike Huffman coding, which relies
on character frequency, LZW dynamically builds a dictionary of substrings, allowing
for efficient encoding. It is particularly effective for text and image compression, as
seen in GIF and TIFF formats.

METHODS:
This project implements LZW compression and decompression in Python with two
primary functions:
●​ Compression (compress_lzw): Maps sequences of characters to dictionary
indexes.
●​ Decompression (decompress_lzw): Reconstructs the original text from
compressed data.

ALGORITHM STEPS:
1.​ Compression:
o​ Initialize a dictionary mapping ASCII characters to their corresponding
codes.
o​Read input text character by character, dynamically building
sequences.
o​ Store sequences as dictionary entries and replace them with
corresponding codes.
2.​ Decompression:
o​ Initialize a dictionary mapping ASCII codes to characters.
o​ Read compressed codes and reconstruct the sequences using
dictionary lookups.
o​ Expand the dictionary dynamically during processing.

PYTHON CODE IMPLEMENTATION:

def compress_lzw(text):
"""Compress the given text using the LZW algorithm."""

# Initialize dictionary with single characters mapped to ASCII codes


dictionary = {chr(i): i for i in range(256)}
next_code = 256 # Next available code in the dictionary
compressed_data = []
current_sequence = ""

for char in text:


combined_sequence = current_sequence + char

if combined_sequence in dictionary:
current_sequence = combined_sequence
else:
compressed_data.append(dictionary[current_sequence])
dictionary[combined_sequence] = next_code
next_code += 1
current_sequence = char # Start new sequence

# Ensure the last sequence is added


if current_sequence:
compressed_data.append(dictionary[current_sequence])

return compressed_data

def decompress_lzw(compressed_data):
"""Decompress the given LZW compressed data back to original text."""
# Initialize dictionary with ASCII characters
dictionary = {i: chr(i) for i in range(256)}
next_code = 256
previous_entry = dictionary[compressed_data[0]]
decompressed_text = previous_entry

for code in compressed_data[1:]:


if code in dictionary:
current_entry = dictionary[code]
elif code == next_code: # Handle case where code is the next expected entry
current_entry = previous_entry + previous_entry[0]
else:
raise ValueError("Invalid compressed code detected!")

decompressed_text += current_entry
dictionary[next_code] = previous_entry + current_entry[0]
next_code += 1
previous_entry = current_entry

return decompressed_text

if __name__ == "__main__":
user_text = input("\nEnter some text to compress: ").strip()

if not user_text:
print("No input detected! Please enter some text.")
else:
compressed_result = compress_lzw(user_text)
print("\nCompression Successful!")
print("Compressed Data:", compressed_result)

decompressed_result = decompress_lzw(compressed_result)
print("\nDecompression Successful!")
print("Decompressed Text:", decompressed_result)

OUTPUT:
RESULTS AND ANALYSIS:
The LZW algorithm effectively reduces data size while ensuring lossless recovery.
Tests with repetitive sequences such as "SURESHGRGBEISURESHGRG" demonstrated
significant compression, and decompression successfully restored the original text.

CONCLUSION:
This lab session successfully implemented the LZW compression algorithm in Python,
proving its efficiency for text and binary data. The dynamic dictionary approach
ensures effective encoding, making LZW an essential technique for lossless
compression.

REFERENCES:
●​ Welch, Terry A. "A technique for high-performance data compression."
Computer 17.6 (1984): 8-19.
●​ Lempel, A., & Ziv, J. "Compression of individual sequences via variable-rate
coding." IEEE Transactions on Information Theory (1978).
●​ Python Documentation: https://docs.python.org/3/
ANIMATION:

You might also like