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
Binary file added Assignment 2/2020BTEIT00077_ASSI2/Abhi.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assignment 2/2020BTEIT00077_ASSI2/compressed.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
132 changes: 132 additions & 0 deletions Assignment 2/2020BTEIT00077_ASSI2/huffman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@

import re
import numpy as np
from PIL import Image
print("Huffman Compression Program")

file = "Abhi.jpeg"
my_string = np.asarray(Image.open(file), np.uint8)
shape = my_string.shape
a = my_string

my_string = str(my_string.tolist())


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

nodes = []
while len(letters) > 0:
nodes.append(letters[0:2])
letters = letters[2:]
nodes.sort()
huffman_tree = []
huffman_tree.append(nodes)


def combine_nodes(nodes):
pos = 0
newnode = []
if len(nodes) > 1:
nodes.sort()
nodes[pos].append("1")
nodes[pos+1].append("0")
combined_node1 = (nodes[pos][0] + nodes[pos+1][0])
combined_node2 = (nodes[pos][1] + nodes[pos+1][1])
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


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)
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]:
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:")


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
for letter in letter_binary:
if code == letter[1]:
uncompressed_string = uncompressed_string+letter_binary[pos][0]
code = ""
pos += 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('original.png')
if a.all() == res.all():
print("Success")
91 changes: 91 additions & 0 deletions Assignment 2/2020BTEIT00077_ASSI2/huffmanres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
Huffman Compression Program
Huffman tree with merged pathways:
Level 0 : [[324797, ' ,6734518092[]']]
Level 1 : [[131070, ' ,', '1'], [193727, '6734518092[]', '0']]
Level 2 : [[80799, '67345', '1'], [112928, '18092[]', '0']]
Level 3 : [[65535, ' ', '1'], [65535, ',', '0'], [112928, '18092[]', '0']]
Level 4 : [[50520, '1809', '1'], [62408, '2[]', '0'], [65535, ',', '0']]
Level 5 : [[38653, '6734', '1'], [42146, '5', '0'], [62408, '2[]', '0'], [65535, ',', '0']]
Level 6 : [[29382, '2', '1'], [33026, '[]', '0'], [42146, '5', '0'], [65535, ' ', '1']]
Level 7 : [[23643, '1', '1'], [26877, '809', '0'], [33026, '[]', '0'], [42146, '5', '0'], [65535, ',', '0']]
Level 8 : [[18426, '67', '1'], [20227, '34', '0'], [26877, '809', '0'], [33026, '[]', '0'], [65535, ' ', '1']]
Level 9 : [[16513, '[', '1'], [16513, ']', '0'], [20227, '34', '0'], [26877, '809', '0'], [42146, '5', '0'], [65535, ',', '0']]
Level 10 : [[10909, '8', '1'], [15968, '09', '0'], [16513, ']', '0'], [20227, '34', '0'], [29382, '2', '1'], [65535, ' ', '1']]
Level 11 : [[9766, '3', '1'], [10461, '4', '0'], [15968, '09', '0'], [16513, ']', '0'], [23643, '1', '1'], [42146, '5', '0'], [65535, ',', '0']]
Level 12 : [[9100, '6', '1'], [9326, '7', '0'], [10461, '4', '0'], [15968, '09', '0'], [16513, ']', '0'], [29382, '2', '1'], [65535, ' ', '1']]
Level 13 : [[7158, '0', '1'], [8810, '9', '0'], [9326, '7', '0'], [10461, '4', '0'], [16513, '[', '1'], [23643, '1', '1'], [42146, '5', '0'], [65535, ',', '0']]

[['[', '00001'], ['4', '01100'], [',', '10'], [' ', '11'], ['2', '0001'], ['5', '010'], [']', '00000'], ['6', '01111'], ['0', '001001'], ['3', '01101'], ['7', '01110'], ['9', '001000'], ['8', '00101'], ['1', '0011']]
Binary code generated:
[ 00001
4 01100
, 10
11
2 0001
5 010
] 00000
6 01111
0 001001
3 01101
7 01110
9 001000
8 00101
1 0011
Your message as binary is:
Your original file size was 2273579 bits. The compressed size is: 1109426
This is a saving of 1164153 bits
Compressed file generated as compressed.txt
Decoding.......
[[[ 44 44 44 255]
[ 60 60 60 255]
[ 37 37 37 255]
...
[ 37 37 37 255]
[ 37 37 37 255]
[ 38 38 38 255]]

[[ 34 34 34 255]
[ 36 36 36 255]
[ 67 67 67 255]
...
[ 43 43 43 255]
[ 45 45 45 255]
[ 43 43 43 255]]

[[ 46 46 46 255]
[ 58 58 58 255]
[ 41 41 41 255]
...
[ 61 61 61 255]
[ 58 58 58 255]
[ 47 47 47 255]]

...

[[ 3 3 3 255]
[ 4 4 4 255]
[ 4 4 4 255]
...
[ 78 78 78 255]
[ 73 73 73 255]
[ 75 75 75 255]]

[[ 3 3 3 255]
[ 4 4 4 255]
[ 4 4 4 255]
...
[ 84 84 84 255]
[ 65 65 65 255]
[ 74 74 74 255]]

[[ 4 4 4 255]
[ 4 4 4 255]
[ 4 4 4 255]
...
[ 85 85 85 255]
[ 71 71 71 255]
[ 74 74 74 255]]]
Observe the shapes and input and output arrays are matching or not
Input image dimensions: (128, 128, 4)
Output image dimensions: (128, 128, 4)
Success
34 changes: 34 additions & 0 deletions Assignment 2/2020BTEIT00077_ASSI2/observation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//Abhijeet Ashok Patil
//2020BTEIT00077





vector quantization -----

compression ratio = (uncompressed image size / compressed image size)
= (22.7 /3.62)
=6.27

encoding performance depends upon number of bits in the image,avg time complexity o(nlogn)



Huffman algo code:

i) consist of the huffman.py file which is the of the Huffman Algorithm. It gives the encoded image and bits values after the execution of algorithm.
ii) folder contains "Abhi.jpeg" image which is in th eform of grayscale image.
iii) compressed.txt is the binary encoded image.which include all paths for bits inside image inputted.


inputted image -------- 128*128 pixels
size 22.7 kB


compressed image --------128*128 pixels
size 3.62 kB




40 changes: 40 additions & 0 deletions Assignment 2/2020BTEIT00077_ASSI2/vecquantization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np
import scipy as sp
import matplotlib.pyplot as plot

from sklearn import cluster

from PIL import Image

img = Image.open("Abhi.jpeg")
img = np.array(img)


n_clusters = 5
np.random.seed(0)

X = img.reshape((-1, 1))
k_means = cluster.KMeans(n_clusters=n_clusters, n_init=4)
k_means.fit(X)
values = k_means.cluster_centers_.squeeze()
labels = k_means.labels_

# array from labels and values
img_compressed = np.choose(labels, values)
img_compressed.shape = img.shape

vmin = img.min()
vmax = img.max()

#inputted uncompressed image
plot.figure(1, figsize=(3, 2.2))
plot.imshow(img.astype('uint8'), cmap=plot.cm.gray, vmin=vmin, vmax=256, )

# compressed image
plot.figure(2, figsize=(3, 2.2))
plot.imshow(img_compressed.astype('uint8'),
cmap=plot.cm.gray, vmin=vmin, vmax=vmax, )
Image.fromarray((img_compressed).astype("uint8")).save("compressed.png")


plot.show()