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):
[Link] = left
[Link] = right
[Link] = parent
[Link] = weight
[Link] = code
def picture_convert(filename, newfilename):
picture = [Link](filename)
picture = [Link]('L')
[Link](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:[Link])
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
[Link](low_node0)
[Link](low_node1)
[Link](new_change_node)
listnode = sorted(listnode, key=lambda node:[Link])
return listnode
def Huffman_Coding(picture):
width = [Link][0]
height = [Link][1]
im = [Link]()
list =[]
for i in range(width):
for j in range(height):
[Link](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([Link],"")
while new_change_node !=head:
if new_change_node.[Link] == new_change_node:
coding_table[[Link]] = "1" + coding_table[[Link]]
else:
coding_table[[Link]] = "0" + coding_table[[Link]]
new_change_node = new_change_node.parent
res_str = "source pixel \t \t code strength after encoding \n"
coding_result = [Link]((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')
[Link](res_str)
return coding_table, coding_result
def Decoding (width,height,coding_table,coding_result):
code_read_now=''
new_pixel =[]
i = 0
decode_image = [Link]( '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( '[Link]', quality=20, optimize=True)
print("Decoding has been completed: the picture is stored as [Link]")
picture = picture_convert('[Link]', '[Link]')
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