0% found this document useful (0 votes)
74 views4 pages

Huffman Coding Using Python

The document discusses Huffman coding using Python. It describes how to convert an image to grayscale, calculate pixel frequencies, construct a Huffman tree from the frequencies and generate codes, encode the image pixels using the codes, and decode the encoded image.

Uploaded by

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

Huffman Coding Using Python

The document discusses Huffman coding using Python. It describes how to convert an image to grayscale, calculate pixel frequencies, construct a Huffman tree from the frequencies and generate codes, encode the image pixels using the codes, and decode the encoded image.

Uploaded by

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

Huffman Coding using Python

JAYANA SRIKAR
RA2011003011313
GIVEN CODE:

from PIL import Image


from numpy import *
import numpy as np

class node:
def __init__(self, right=None, left=None, parent=None, weight=0,
code=None):
self.left = left
self.right = right
self.parent = parent
self.weight = weight
self.code = code

def picture_convert(filename, newfilename):


picture = Image.open(filename)
picture = picture.convert('L')
picture.save(newfilename)
return picture

def pixel_number_caculate(list):
pixel_number={}
for i in list:
if i not in pixel_number. keys():
pixel_number[i]=1
else:
pixel_number[i] += 1
return pixel_number

def node_construct(pixel_number):
node_list =[]
for i in range(len(pixel_number)):
node_list.append(node(weight=pixel_number[i]
[1],code=str(pixel_number[i][0])))
return node_list

def node_construct(pixel_number):
node_list =[]
for i in range(len(pixel_number)):
node_list.append(node(weight=pixel_number[i]
[1],code=str(pixel_number[i][0])))
return node_list

def tree_construct(listnode):
listnode = sorted(listnode, key=lambda node:node.weight)
while len(listnode) != 1:
low_node0,low_node1 = listnode[0], listnode[1]
new_change_node = node()
new_change_node.weight = low_node0.weight + low_node1.weight
new_change_node.left = low_node0
new_change_node.right = low_node1
low_node0.parent = new_change_node
low_node1.parent = new_change_node
listnode.remove(low_node0)
listnode.remove(low_node1)
listnode.append(new_change_node)
listnode = sorted(listnode, key=lambda node:node.weight)
return listnode

def Huffman_Coding(picture):
width = picture.size[0]
height = picture.size[1]
im = picture.load()

list =[]
for i in range(width):
for j in range(height):
list.append(im[i,j])

pixel_number = pixel_number_caculate(list)
pixel_number =sorted(pixel_number.items(),key=lambda item:item[1])

node_list = node_construct(pixel_number)
head = tree_construct(node_list)[0]
coding_table = {}
for e in node_list:
new_change_node = e
coding_table.setdefault(e.code,"")
while new_change_node !=head:
if new_change_node.parent.left == new_change_node:
coding_table[e.code] = "1" + coding_table[e.code]
else:
coding_table[e.code] = "0" + coding_table[e.code]
new_change_node = new_change_node.parent

res_str = "source pixel \t \t code strength after encoding \n"


coding_result = np.zeros((512,512))
for i in range(width):
for j in range(height):
for key,values in coding_table.items():
if str(im[i,j]) == key:
coding_result[i][j] = values
res_str += key +"\t \t \t \t \t \t" + values + "\n"
file = open('huff_coding_table_result.txt','w')
file.write(res_str)
return coding_table, coding_result

def Decoding (width,height,coding_table,coding_result):


code_read_now=''
new_pixel =[]
i = 0
decode_image = Image.new( 'L' ,(width, height))

print("New Pix ", new_pixel, type(new_pixel))


# for pix in coding_table.keys():
for i in range(width):
for j in range(height):
decode_image.putpixel((i,j),(int(coding_result[i][j])))
decode_image.save( 'decode.jpg', quality=20, optimize=True)
print("Decoding has been completed: the picture is stored as decode.jpg")

picture = picture_convert('lenargb.jpg', 'new.jpg')


cod_table, cod_res = Huffman_Coding(picture)
Decoding(512,512,cod_table,cod_res)

STEPS:

1) Run the following code with the image in any code editor
2) After running the code its show 2 images files and Huffman code
text file as shown in below image

3)
4) AFTER RUN THE PROGRAM THE INPUT IMAGE , OUT PUT IMAGE AND DECODE
IMAGE WILL BE LIKE THIS

INPUT IMAGE

OUTPUT IMAGE

DECODE IMAGE AFTER HUFFMAN CODING

5) AFTER THE RUNNING THE PROGRAM THE HUFFMAN CODING TEXT FILE IT
WILL SPECIFY THE SOURCE PIXEL OF THE IMAGE AND CODE STRENGH AFTER
ENCODING THE IMAGE

You might also like