From da31031b66d366239c878cb0202b3288ac360ab6 Mon Sep 17 00:00:00 2001 From: PSupriya12 <90033745+PSupriya12@users.noreply.github.com> Date: Tue, 9 Aug 2022 11:26:54 +0530 Subject: [PATCH 1/3] Create CA2 --- CA2 | 1 + 1 file changed, 1 insertion(+) create mode 100644 CA2 diff --git a/CA2 b/CA2 new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/CA2 @@ -0,0 +1 @@ + From 3551dc951d9d8f808837bf3179d39081883e9770 Mon Sep 17 00:00:00 2001 From: PSupriya12 <90033745+PSupriya12@users.noreply.github.com> Date: Tue, 9 Aug 2022 11:40:36 +0530 Subject: [PATCH 2/3] Add files via upload --- Assignment 2/HuffmanEncoding.py | 131 +++++++++++++++++++++++++++++ Assignment 2/Observation.txt | 33 ++++++++ Assignment 2/OriginalGreyImage.png | Bin 0 -> 1968 bytes Assignment 2/ReconstructedImg.png | Bin 0 -> 467 bytes Assignment 2/VectorQuantization.py | 54 ++++++++++++ 5 files changed, 218 insertions(+) create mode 100644 Assignment 2/HuffmanEncoding.py create mode 100644 Assignment 2/Observation.txt create mode 100644 Assignment 2/OriginalGreyImage.png create mode 100644 Assignment 2/ReconstructedImg.png create mode 100644 Assignment 2/VectorQuantization.py diff --git a/Assignment 2/HuffmanEncoding.py b/Assignment 2/HuffmanEncoding.py new file mode 100644 index 0000000..f8f2aa4 --- /dev/null +++ b/Assignment 2/HuffmanEncoding.py @@ -0,0 +1,131 @@ +import re +import numpy as np +from PIL import Image +print("Huffman Compression Program") + +file = "output.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('previous.png') +if a.all() == res.all(): + print("Success") \ No newline at end of file diff --git a/Assignment 2/Observation.txt b/Assignment 2/Observation.txt new file mode 100644 index 0000000..de2fbae --- /dev/null +++ b/Assignment 2/Observation.txt @@ -0,0 +1,33 @@ +Name : Supriya Manikrao Pawar +PRN : 2020BTEIT00052 +Batch : T5 +Course : Computer Algorithm + +Assignment No:2 +Vector Quantization and Huffman Encoding + +Problem Statement: To use the Vector Quantization Algorithm to compressed an image. + Get the Huffman code of the input image using Huffman Encoding Algorithm. + +Quantization means approximation. +Here for this I have chosen 240*240 pixels PNG image which of 2 kb originally. +I have compressed this image using vector quantization and the reconstructed size is 1 kb. + +Files: + Images: + 1.OriginalGreyImage.png is the image before compressing + 2.ReconstructedImg.png is the image after compressing + + Coding: + 1.VectorQuatization is the file written in Python which contains the code for compressing image. + 2.HuffmanEncoding is the file in Python. + I have created these files by referring on the google and youTube. + + Observations: + File is reduced from 2 kb to 1 kb after compression. + Compression rate: Original image/Compressed image + = 2kb /1 kb + = 2 + + The time complexity for Huffman algorithm is O(nlogn). + n is the number of nodes of tree. \ No newline at end of file diff --git a/Assignment 2/OriginalGreyImage.png b/Assignment 2/OriginalGreyImage.png new file mode 100644 index 0000000000000000000000000000000000000000..da394ace28860da54c36bf0bc68697335184cddd GIT binary patch literal 1968 zcmeH|iBr;99Kgkk^31H&bkS@HZ9K3wGgB-#lgKEF@3dGGta^L@wnedqK2z9K&#q^_2UmWqms zF6x5EWk4?M^gWuu=yqim4+zajFQkXc_D-wpt|O?ZfN!Ea-2cd|knnu#gKt9mC#A84 zwHjz0t4ia*y9Ka$-Q#Ukdf$3oLvT!S>7y~NlgIpTGmd&&qsr{|xbOYYS5W=C;q~fq zU?tSz@Rx%Zjvm=(m6w>>xzbSy`;az0NObX6w6dBJ(%Y=0Wd^Y`m4yP_P*DlLM#ZK- zXLBW}`3smJd^tY&vG5;kT25e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diff --git a/Assignment 2/ReconstructedImg.png b/Assignment 2/ReconstructedImg.png new file mode 100644 index 0000000000000000000000000000000000000000..fd1cbc4ea63580e8d5d0efca5708f8d8c6f233be GIT binary patch literal 467 zcmeAS@N?(olHy`uVBq!ia0vp^A3&Ie8AzU~{<08Ai3a$DxHiB*V`C$P>0aQI_4-&O zPyu^MkYDisRqH&T^)i&sdAEgufica~#WAGf*4w)q`?Y4x--GtS!Ue`s~~SdsQ9%Zq_I`@!E#>uA5<(?4wJjx5fYcQ{@% z&F`XZ^KXGARW0pH8oHk`>N!t(8vjFRkxc#JSUvuryY{9&(AF5h{U&Mbhw`sBPZL_~xF!f=#lwI16u*2HscUC>z zDy?_*!y(@K6%SPns0}`3?+7{kt?fb>@9pS>Y}1_lUt6bYeb3raB%XeI)uGy_FVc1Q z6pB75|9 Date: Tue, 9 Aug 2022 11:56:33 +0530 Subject: [PATCH 3/3] Delete CA2 --- CA2 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 CA2 diff --git a/CA2 b/CA2 deleted file mode 100644 index 8b13789..0000000 --- a/CA2 +++ /dev/null @@ -1 +0,0 @@ -