Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 02- 2020BTEIT00044 #154

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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/2020BTEIT00044/bridge.jpg
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/2020BTEIT00044/compressd_bridge.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
150 changes: 150 additions & 0 deletions Assignment 2/2020BTEIT00044/huffman_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import re
import numpy as np
from PIL import Image
print("Huffman Compression Program")
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")

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

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")
2 changes: 2 additions & 0 deletions Assignment 2/2020BTEIT00044/observation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Vector quantization (VQ) is a classical quantization technique from signal processing that allows the modeling of probability density functions by the distribution of prototype vectors. It was originally used for data compression.

42 changes: 42 additions & 0 deletions Assignment 2/2020BTEIT00044/vector_quantization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import numpy as np
import scipy as sp

import matplotlib.pyplot as plt

from sklearn import cluster

from PIL import Image

im = Image.open("myImage.jpg")
im = np.array(im)


n_clusters = 5
np.random.seed(0)

X = im.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_


im_compressed = np.choose(labels, values)
im_compressed.shape = im.shape

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


plt.figure(1, figsize=(3, 2.2))
plt.imshow(im.astype('uint8'), cmap=plt.cm.gray, vmin=vmin, vmax=256, )


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


plt.show()