Multimedia Rohit Josh i
Multimedia Rohit Josh i
INSTITUTE OF ENGINEERING
TRIBHUVAN UNIVERSITY
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.
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.
oRead 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.
def compress_lzw(text):
"""Compress the given text using the LZW algorithm."""
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
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
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: