Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions 2020BTEIT00071/VQ.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions 2020BTEIT00071/compressed.txt

Large diffs are not rendered by default.

Binary file added 2020BTEIT00071/compressedhuffman code.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 154 additions & 0 deletions 2020BTEIT00071/huffman code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
Name-Dhanashri Shahaji Maske
PRN-2020BTEIT00071

import re
import numpy as np
from PIL import Image
print("Huffman Compression Program")
print("=================================================================")
h = int(input("Enter 1 if you want to input an colour image file, 2 for default gray scale case:"))
if h == 1:
file = input("Enter the filename:")
my_string = np.asarray(Image.open(file),np.uint8)
shape = my_string.shape
a = my_string
print ("Enetered string is:",my_string)
my_string = str(my_string.tolist())
elif h == 2:
array = np.arange(0, 737280, 1, np.uint8)
my_string = np.reshape(array, (1024, 720))
print ("Enetered string is:",my_string)
a = my_string
my_string = str(my_string.tolist())

else:
print("You entered invalid input") # taking user input

letters = []
only_letters = []
for letter in my_string:
if letter not in letters:
frequency = my_string.count(letter) #frequency of each letter repetition
letters.append(frequency)
letters.append(letter)
only_letters.append(letter)

nodes = []
while len(letters) > 0:
nodes.append(letters[0:2])
letters = letters[2:] # sorting according to frequency
nodes.sort()
huffman_tree = []
huffman_tree.append(nodes) #Make each unique character as a leaf node

def combine_nodes(nodes):
pos = 0
newnode = []
if len(nodes) > 1:
nodes.sort()
nodes[pos].append("1") # assigning values 1 and 0
nodes[pos+1].append("0")
combined_node1 = (nodes[pos] [0] + nodes[pos+1] [0])
combined_node2 = (nodes[pos] [1] + nodes[pos+1] [1]) # combining the nodes to generate pathways
newnode.append(combined_node1)
newnode.append(combined_node2)
newnodes=[]
newnodes.append(newnode)
newnodes = newnodes + nodes[2:]
nodes = newnodes
huffman_tree.append(nodes)
combine_nodes(nodes)
return huffman_tree # huffman tree generation

newnodes = combine_nodes(nodes)

huffman_tree.sort(reverse = True)
print("Huffman tree with merged pathways:")

checklist = []
for level in huffman_tree:
for node in level:
if node not in checklist:
checklist.append(node)
else:
level.remove(node)
count = 0
for level in huffman_tree:
print("Level", count,":",level) #print huffman tree
count+=1
print()

letter_binary = []
if len(only_letters) == 1:
lettercode = [only_letters[0], "0"]
letter_binary.append(letter_code*len(my_string))
else:
for letter in only_letters:
code =""
for node in checklist:
if len (node)>2 and letter in node[1]: #genrating binary code
code = code + node[2]
lettercode =[letter,code]
letter_binary.append(lettercode)
print(letter_binary)
print("Binary code generated:")
for letter in letter_binary:
print(letter[0], letter[1])

bitstring =""
for character in my_string:
for item in letter_binary:
if character in item:
bitstring = bitstring + item[1]
binary ="0b"+bitstring
print("Your message as binary is:")
# binary code generated

uncompressed_file_size = len(my_string)*7
compressed_file_size = len(binary)-2
print("Your original file size was", uncompressed_file_size,"bits. The compressed size is:",compressed_file_size)
print("This is a saving of ",uncompressed_file_size-compressed_file_size,"bits")
output = open("compressed.txt","w+")
print("Compressed file generated as compressed.txt")
output = open("compressed.txt","w+")
print("Decoding.......")
output.write(bitstring)

bitstring = str(binary[2:])
uncompressed_string =""
code =""
for digit in bitstring:
code = code+digit
pos=0 #iterating and decoding
for letter in letter_binary:
if code ==letter[1]:
uncompressed_string=uncompressed_string+letter_binary[pos] [0]
code=""
pos+=1

print("Your UNCOMPRESSED data is:")
if h == 1:
temp = re.findall(r'\d+', uncompressed_string)
res = list(map(int, temp))
res = np.array(res)
res = res.astype(np.uint8)
res = np.reshape(res, shape)
print(res)
print("Observe the shapes and input and output arrays are matching or not")
print("Input image dimensions:",shape)
print("Output image dimensions:",res.shape)
data = Image.fromarray(res)
data.save('uncompressed.png')
if a.all() == res.all():
print("Success")
if h == 2:
temp = re.findall(r'\d+', uncompressed_string)
res = list(map(int, temp))
print(res)
res = np.array(res)
res = res.astype(np.uint8)
res = np.reshape(res, (1024, 720))
print(res)
data = Image.fromarray(res)
data.save('uncompressed.png')
print("Success")
50 changes: 50 additions & 0 deletions 2020BTEIT00071/observation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Name:Dhanashri Shahaji Maske
PRN: 2020BTEIT00071


Title: To use the Vector Quantization Algorithm to compressed an 128x128 px image.
Also get the Huffman code of the inputed image using Huffman Encoding Algorithm.


First an introduction to my file structure:

1) Huffman folder:
i) It consist of the huffman.c file which is the contains the C code of the Huffman Algorithm. It gives the encoded image and bits values after the execution.
ii) It was referred from Geek for Geeks.
iii) The folder also has a "gray.bmp" image which is used my grayscale image.
iv) encode.txt has the binary encoded image of the "gray.bmp" inside it.
v) Huffman tree path for all the bits present inside the "gray.bmp" (commonly ranges from 0-254) is stored in huffman_codes.txt.

2) Images Folder:
i) It has two images "gray.bmp" (original and uncompressed) and "gray_compressed.bmp" (original and commpressed).
ii) Properties of "gray.bmp" :
Dimensions: 128x128 px
Size: 17 kB

iii) Properties of "gray_compressed.bmp" :
Dimensions: 131x33 px
Size:9 kB

3) VQ folder:
i) This folder has the Vector Quantization Algorithm in it which is C++ language.
ii) The source code is referred from a git repository.
iii) Source.cpp provides two clones of the original image in compressed formed.

Performance:

Vector Quantization Algorithm:

By calculating the Compression Ratio of the image:

compression ratio = (uncompressed image size / compressed image size)
= (17 / 9)
= 1.88

Thus VQ is one of the lossy compression algorithms and which is clearly visible by looking the compression ratio.

Huffman Encoding Algorithm:

The encoding performance for the Huffman is on the number of various bits present inside the image.

The general Time Complexity of the algorithm is O(nlogn), where n is number of different nodes present inside the Huffman tree.

Binary file added 2020BTEIT00071/original.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.