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

21620010 #136

Open
wants to merge 4 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 not shown.
Binary file not shown.
41 changes: 41 additions & 0 deletions Assignment 2/21620010- Assignment 2/Vector_Quantization.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import numpy as np
import scipy as sp
# For plotting and Visualization
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_

# create an array from labels and values
im_compressed = np.choose(labels, values)
im_compressed.shape = im.shape

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

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

# compressed image
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()
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/21620010- Assignment 2/haffman_coding.txt
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")
print("=================================================================")

file = "compressed.png"
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")
24 changes: 24 additions & 0 deletions Assignment 2/21620010- Assignment 2/observation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Name: Shikha S. Choudhari
Prn. 21620010


1) Original.jpg :
Dimensions: 128x128 px
Size: 18.7 kB
2) Compressed.png :
Dimensions: 80x76 px
Size: 4.28 kB

Performance:
Vector Quantization Algorithm:
By calculating the Compression Ratio of the image:

compression ratio = (uncompressed image size / compressed image size)
Compression Ratio = 18.7/4.28
= 4.36


Huffman Encoding Time Complexity:

The time complexity for encoding each unique character based on its frequency is O(nlog n).
Extracting minimum frequency from the priority queue takes place 2*(n-1) times and its complexity is O(log n). Thus the overall complexity is O(nlog n).
Binary file added Assignment 2/21620010- Assignment 2/original.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.