-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressionMain.py
More file actions
266 lines (233 loc) · 11 KB
/
CompressionMain.py
File metadata and controls
266 lines (233 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import sys
import tkinter as tk
from tkinter import filedialog, ttk
from PIL import ImageTk, Image, ImageDraw
import numpy as np
import RLE
import RLEBitPlane
import HuffMan
import bitstring
import LZW
import time
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_uploadbutton()
self.createRLEButtons()
self.createTextBox()
self.createRLEBitPlaneButtons()
self.createHuffmanButtons()
self.createLZWButtons()
def createTextBox(self):
self.inputString = tk.Text(height=1, width=15)
self.inputString.pack()
def create_uploadbutton(self):
self.labelFrame = ttk.LabelFrame(self, text="Open A File")
self.labelFrame.grid(column=0, row=3, padx=10, pady=10)
self.uploadButton = ttk.Button(self.labelFrame, text="Browse A File", command=self.uploadButtonAction)
self.uploadButton.grid(column=0, row=4)
def createHuffmanButtons(self):
self.huffmanButton = tk.Button(self, text="Huffman Encoding", command=self.huffmanEncode)
self.huffmanButton.grid(column=0, row=50, padx=10, pady=10)
self.huffmanDecodeButton = tk.Button(self, text="Huffman Decoding", command=self.huffmanDecode)
self.huffmanDecodeButton.grid(column=2, row=50, padx=10, pady=10)
def createLZWButtons(self):
self.LZWButton = tk.Button(self, text="LZW Encoding", command=self.LZWEncode)
self.LZWButton.grid(column=0, row=60, padx=10, pady=10)
self.LZWDecodeButton = tk.Button(self, text="LZW Decoding", command=self.LZWDecode)
self.LZWDecodeButton.grid(column=2, row=60, padx=10, pady=10)
def uploadButtonAction(self):
self.filename = filedialog.askopenfilename()
self.label = ttk.Label(self.labelFrame, text="")
self.label.grid(column=1, row=2)
self.label.configure(text=self.filename)
self.show_original_image()
def createRLEButtons(self):
self.RLEButton = tk.Button(self, text="Run Length Encoding", command=self.RLEButtonAction)
self.RLEButton.grid(column=0, row=30, padx=10, pady=10)
self.RLEDecodeButton = tk.Button(self, text="Run Length Decoding", command=self.RLEDecodeButtonAction)
self.RLEDecodeButton.grid(column=2, row=30, padx=10, pady=10)
def createRLEBitPlaneButtons(self):
self.RLEBitPlaneButton = tk.Button(self, text="RLE Bit Plane Encoding", command=self.RLEBitPlaneButtonAction)
self.RLEBitPlaneButton.grid(column=0, row=40, padx=10, pady=10)
self.RLEBitPlaneDecodeButton = tk.Button(self, text="RLE Bit Plane Decoding", command=self.RLEBitPlaneDecodeButtonAction)
self.RLEBitPlaneDecodeButton.grid(column=2, row=40, padx=10, pady=10)
def show_original_image(self):
img = ImageTk.PhotoImage(Image.open(self.filename))
newWindow = tk.Toplevel(root)
newWindow.title("Original Image")
newWindow.geometry("512x512")
newWindow.configure(background='grey')
newWindow.canvas = tk.Canvas(newWindow, width=800, height=800)
newWindow.img = ImageTk.PhotoImage(Image.open(self.filename))
newWindow.canvas.create_image(10, 10, anchor=tk.NW, image=img)
newWindow.canvas.image = img
newWindow.canvas.pack()
newWindow.mainloop()
def RLEBitPlaneButtonAction(self):
oldimagearray = self.convert_to_array()
oldArrayFlattened = oldimagearray.flatten()
start = time.time()
self.RLEBitPlaneEncode = RLEBitPlane.encode(oldArrayFlattened)
end = time.time()
self.RLEBitPlaneEncode = np.asarray(self.RLEBitPlaneEncode)
execTime = self.compressionRatio(sys.getsizeof(self.RLEBitPlaneEncode), sys.getsizeof(oldArrayFlattened))
#oldArrayFlattened = oldArrayFlattened.astype(np.uint16)
print("-----------------------------------------------------------")
print("Run Length Encoding Size in Bit Plane: ")
print(sys.getsizeof(self.RLEBitPlaneEncode))
print("Encoded type (numpy array with lists):")
print(self.RLEBitPlaneEncode.dtype)
print("Original Array size: ")
print(sys.getsizeof(oldArrayFlattened))
print("old array type:")
print(oldArrayFlattened.dtype)
print("Compression Ratio:")
print(str(execTime) + "%")
print("Execution Time:")
print(end-start)
print("-----------------------------------------------------------")
def RLEBitPlaneDecodeButtonAction(self):
oldimagearray = self.convert_to_array()
start = time.time()
decoded = np.asarray(RLEBitPlane.decode(self.RLEBitPlaneEncode))
end = time.time()
decoded = decoded.reshape((len(oldimagearray), len(oldimagearray[0])))
print("-----------------------------------------------------------")
print("Decode Execution Time:")
print(end-start)
print("-----------------------------------------------------------")
self.show_new_image(decoded, "RLE Bit Plane Decoded")
def RLEButtonAction(self):
#inputString = self.inputString.get("1.0", tk.END)
oldimagearray = self.convert_to_array()
oldArrayFlattened = oldimagearray.flatten()
#oldArrayFlattened = np.fromstring(inputString, sep=',', dtype=int)
startTime = time.time()
self.RLEoutput = RLE.encode(oldArrayFlattened)
endTime = time.time()
oldArrayFlattened = oldArrayFlattened.astype(np.uint16)
compressionRatio = self.compressionRatio(sys.getsizeof(self.RLEoutput), sys.getsizeof(oldArrayFlattened))
print("-----------------------------------------------------------")
print("Run Length Encoding 1D array Size: ")
print(sys.getsizeof(self.RLEoutput))
print("Encoded array type:")
print(self.RLEoutput.dtype)
print("Original Image 1D array size: ")
print(sys.getsizeof(oldArrayFlattened))
print("old array type:")
print((oldArrayFlattened.dtype))
print("Compression Ratio:")
print(str(compressionRatio) + "%")
print("Execution Time:")
print(endTime-startTime)
print("-----------------------------------------------------------")
def RLEDecodeButtonAction(self):
oldimagearray = self.convert_to_array()
startTime = time.time()
decodedArray = RLE.decode(self.RLEoutput)
endTime = time.time()
#decodedArray2D = np.reshape(decodedArray, (-1, 2))
#self.show_new_image(decodedArray2D, "RLE Decode")
B = decodedArray.reshape((len(oldimagearray), len(oldimagearray[0])))
print("-----------------------------------------------------------")
print("Decode Time:")
print(endTime-startTime)
print("-----------------------------------------------------------")
self.show_new_image(B, "RLE Decoded")
def huffmanEncode(self):
oldimagearray = self.convert_to_array()
oldArrayFlattened = list(oldimagearray.flatten())
start = time.time()
self.hm = HuffMan.Picture(self.filename)
self.hm.load_data()
self.hm.make_heap()
self.hm.merge_nodes()
self.hm.heaporder(self.hm.heap[0], "")
self.hm.create_compression_keys()
self.HMencoded = self.hm.writeout()
self.hm.readin()
#tmp = " ".join(str(e) for e in self.HMencoded)
#b = bitstring.BitArray("0b" + tmp)
end = time.time()
compR = self.compressionRatio(sys.getsizeof(self.HMencoded), sys.getsizeof(oldArrayFlattened))
print("-----------------------------------------------------------")
print("Huffman encoded string size:")
print(sys.getsizeof(self.HMencoded))
print("Encoded type:")
print(type(self.HMencoded))
print("Original string size:")
print(sys.getsizeof(oldArrayFlattened))
print("Original type:")
print(type(oldArrayFlattened))
print("Compression Ratio:")
print(str(compR)+"%")
print("Execution Time:")
print(end-start)
print("-----------------------------------------------------------")
def huffmanDecode(self):
oldimagearray = self.convert_to_array()
start = time.time()
decodedImg = self.hm.create_new_image()
end = time.time()
decodedImg.show()
print("-----------------------------------------------------------")
print("Decode execution time:")
print(end - start)
print("-----------------------------------------------------------")
def LZWEncode(self):
start = time.time()
inputString = self.inputString.get("1.0", tk.END)
self.LZWEncoded, self.LZWDictionary = LZW.compress(inputString)
end = time.time()
inputString = list(inputString)
compR = self.compressionRatio(sys.getsizeof(self.LZWEncoded), sys.getsizeof(inputString))
print("-----------------------------------------------------------")
print("LZW Encoded string:")
print(self.LZWEncoded)
print("Encoded size:")
print(sys.getsizeof(self.LZWEncoded))
print("Original size:")
print(sys.getsizeof(inputString))
print("Compression Ratio:")
print(str(compR) + "%")
print("LZW Execution time:")
print(end - start)
print("Dictionary:")
print(self.LZWDictionary)
print("-----------------------------------------------------------")
def LZWDecode(self):
start = time.time()
LZWDecoded = LZW.decompress(self.LZWEncoded, self.LZWDictionary)
end = time.time()
print("-----------------------------------------------------------")
print("LZW Decoded string:")
print(LZWDecoded)
print("LZW Execution time:")
print(end - start)
print("-----------------------------------------------------------")
def convert_to_array(self):
img = Image.open(self.filename).convert('L')
newimgarray = np.array(img)
return newimgarray
def show_new_image(self, args, method):
newimgarray = Image.fromarray(np.asarray(args, np.uint8))
img = ImageTk.PhotoImage(Image.fromarray(np.asarray(newimgarray)))
newWindow = tk.Toplevel(root)
newWindow.title(method)
newWindow.geometry("512x512")
newWindow.configure(background='grey')
newWindow.canvas = tk.Canvas(newWindow, width=800, height=800)
newWindow.img = ImageTk.PhotoImage(Image.open(self.filename))
newWindow.canvas.create_image(10, 10, anchor=tk.NW, image=img)
newWindow.canvas.image = img
newWindow.canvas.pack()
newWindow.mainloop()
def compressionRatio(self, new, old):
return (new/old) * 100
root = tk.Tk()
root.minsize(600, 400)
app = Application(master=root)
app.mainloop()