PSYCHOCODES

Python Program for LZW (Lempel Ziv Welch) Compression Algorithm

LZW(Lempel Ziv Welch) Algorithm is a universal lossless data compression algorithm( in short it is a compression algo to compress data, as simple as that laughing), It is the algorithm of the widely used Unix file compression utility compress, and is used in the GIF image format.

In this tutorial we will give you a basic overview of LZW compression algorithm and then we will write a python cool code for it  and if you want us to make a full tutorial on explaining LZW algorithm then comment down in the comments section of this article.

LZW Algorithm

A high level view of the encoding algorithm is shown here:

  • initialize the dictionary to contain all strings of length one.
  • Find the longest string W in the dictionary that matches the current input.
  • Emit the dictionary index for W to output and remove W from the input.
  • Add W followed by the next symbol in the input to the dictionary.
  • Go to Step 2.

So thats the universal Algorithm Abraham Lempel, Jacob Ziv, and Terry Welch created 32 years ago(calculate the year wink ). If you want the full article on LZW on PsychoCodes then do not forget to comment down in the comments section of this post.

Now let's dive straight into the snake's cage(you know what i mean), ok let's start our python code, first we are going to define to functions encodeLzw() and decodeLzw() for encoding and decoding purpose

encodeLzw()

def encodeLzw( input_string ):
	for char in input_string:
		if char in dictionary:	
			continue
		else:
			dictionary.append(char)
	dictionary.sort()
	no_input = len(dictionary)
	next = ""
	for char in input_string:
		a = next + char
		if a in dictionary:
			next = a
		else:
			next = a[len(a)-1]
			dictionary_word = a[0:len(a)-1]	
			dictionary.append(a)
			encoded_version.append(dictionary.index(dictionary_word))
			encoded_version.append(dictionary.index(next))

decodeLzw()

def decodeLzw( encoded_version ):
	x = ""	
	for i in encoded_version:
		x = x + dictionary[i]
	return x

Now let's make use of these functions and write a full python code for our algorithm

def encodeLzw( input_string ):
	for char in input_string:
		if char in dictionary:	
			continue
		else:
			dictionary.append(char)
	dictionary.sort()
	no_input = len(dictionary)
	next = ""
	for char in input_string:
		a = next + char
		if a in dictionary:
			next = a
		else:
			next = a[len(a)-1]
			dictionary_word = a[0:len(a)-1]	
			dictionary.append(a)
			encoded_version.append(dictionary.index(dictionary_word))
			encoded_version.append(dictionary.index(next))

def decodeLzw( encoded_version ):
	x = ""	
	for i in encoded_version:
		x = x + dictionary[i]
	return x

dictionary = []
encoded_version = []
input_string = input("Enter a string to encode: ")
encodeLzw(input_string)
decoded_version = decodeLzw(encoded_version)
print("The encoded version of the given string is: ", encoded_version)
print("The Decoded version of the encoded string is: ",decoded_version)

Output

LZW algorithm using Python

Thanks for reading and if you want a full article on LZW algorithm then do not forget to comment down.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don't forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.

Share your thoughts