def LZW_decompress_GRAY_image(height, width, filename): S_T = time.time() o = LZW_image.Decode_from_file(filename) # olist=[] # for i in range(0,len(o)): # olist.append(o[i]) image_1 = LZW_image.Decompress(o) data = [] # # dem=0 for i in image_1: if type(i) is int: data.append(np.uint8(i)) else: l = i.split() for p in l: data.append(np.uint8(p)) temp = np.asarray(data) image_2 = temp.reshape(height, width) E_T = time.time() time_elapsed = (E_T - S_T) * 1000 cv.imshow('DECOMPRESS FROM LZW ALGORITHM', image_2) k = cv.waitKey(0) if k == 27: # wait for ESC key to exit cv.destroyAllWindows() return time_elapsed
def LWZ_compress_GRAY_image(path): img = cv.imread(path, cv.IMREAD_GRAYSCALE) list_data_of_img = np.array(img.ravel()) S_T = time.time() compress = LZW_image.Compress(list_data_of_img) height = img.shape[0] width = img.shape[1] input_bits = width * height * 8 summ = LZW_image.Sum_bit(compress) print("20 element of LWZ array:", compress[0:20]) print("Number of bits required to represent the data before compression:", input_bits) print("Number of bits required to represent the data after compression:", summ) # print("P/s: One number of LWZ array represent by %d to optimal compression ratio." % len_bit) ratio = (input_bits / summ) print("Compression ratio: ", ratio) print("WRITE DATA TO FILE") filename = HM_image.filename_from_path(path) print("File compress will save at folder .\compress_file_LZW\\%s" % filename) filename_compress = os.getcwd() + '\compressed_file_LZW\\' + filename LZW_image.Write_to_file(compress, filename_compress) print("DONE COMPRESSS\n\n\n") E_T = time.time() time_elapsed = (E_T - S_T) * 1000 return time_elapsed, ratio, height, width, filename_compress
def LWZ_Color_C(path): img = cv.imread(path, cv.IMREAD_COLOR) S_T = time.time() # list_data_of_img = np.array(img.ravel()) b, g, r = cv.split(img) xb = list(b.ravel()) xg = list(g.ravel()) xr = list(r.ravel()) b_compress = LZW_image.Compress(xb) g_compress = LZW_image.Compress(xg) r_compress = LZW_image.Compress(xr) height = img.shape[0] width = img.shape[1] input_bits = width * height * 8 * 3 summ = LZW_image.Sum_bit(b_compress) + LZW_image.Sum_bit(g_compress) + LZW_image.Sum_bit(r_compress) print("Number of bits required to represent the data before compression:", input_bits) print("Number of bits required to represent the data after compression by LZW:", summ) ratio = (input_bits / summ) print("Compression ratio: ", ratio) print("WRITE DATA TO FILE") filename = HM_image.filename_from_path(path) filename_compress = os.getcwd() + '\compressed_file_LZW\\' + filename LZW_image.Write_to_file(b_compress, filename_compress+"_B") LZW_image.Write_to_file(g_compress, filename_compress + "_G") LZW_image.Write_to_file(r_compress, filename_compress + "_R") E_T = time.time() time_elapsed = (E_T - S_T) print("DONE LZW COMPRESSS\n\n\n") return time_elapsed, ratio, height, width, filename_compress
def LWZ_Gray_D(height, width, filename): S_T = time.time() o = LZW_image.Decode_from_file(filename) # olist=[] # for i in range(0,len(o)): # olist.append(o[i]) image_1 = LZW_image.Decompress(o) data = [] # # dem=0 for i in image_1: if type(i) is int: data.append(np.uint8(i)) else: l = i.split() for p in l: data.append(np.uint8(p)) temp = np.asarray(data) image_2 = temp.reshape(height, width) E_T = time.time() time_elapsed = (E_T - S_T) return time_elapsed
def LWZ_decompress_for_COLOR_image(height, width, filename): S_T = time.time() b_list = LZW_image.Decode_from_file(filename + "_B") r_list = LZW_image.Decode_from_file(filename + "_R") g_list = LZW_image.Decode_from_file(filename + "_G") temp_b = LZW_image.Decompress(b_list) temp_r = LZW_image.Decompress(r_list) temp_g = LZW_image.Decompress(g_list) data_b = [] data_r = [] data_g = [] # # dem=0 for i in temp_b: if type(i) is int: data_b.append(np.uint8(i)) else: l = i.split() for p in l: data_b.append(np.uint8(p)) # R for i in temp_r: if type(i) is int: data_r.append(np.uint8(i)) else: l = i.split() for p in l: data_r.append(np.uint8(p)) # G for i in temp_g: if type(i) is int: data_g.append(np.uint8(i)) else: l = i.split() for p in l: data_g.append(np.uint8(p)) temp1 = np.asarray(data_b) temp2 = np.asarray(data_g) temp3 = np.asarray(data_r) img_B = temp1.reshape(height, width) img_G = temp2.reshape(height, width) img_R = temp3.reshape(height, width) img = cv.merge((img_B, img_G, img_R)) E_T = time.time() time_elapsed = (E_T - S_T) * 1000 cv.imshow('DECOMPRESSS BY LZW ALGORITHM', img) k = cv.waitKey(0) if k == 27: # wait for ESC key to exit cv.destroyAllWindows() print("DONEEEEEEEEE\n\n\n") return time_elapsed
def LWZ_Color_D(height, width, filename): S_T = time.time() b_list = LZW_image.Decode_from_file(filename + "_B") r_list = LZW_image.Decode_from_file(filename + "_R") g_list = LZW_image.Decode_from_file(filename + "_G") temp_b = LZW_image.Decompress(b_list) temp_r = LZW_image.Decompress(r_list) temp_g = LZW_image.Decompress(g_list) data_b = [] data_r = [] data_g = [] # # dem=0 for i in temp_b: if type(i) is int: data_b.append(np.uint8(i)) else: l = i.split() for p in l: data_b.append(np.uint8(p)) # R for i in temp_r: if type(i) is int: data_r.append(np.uint8(i)) else: l = i.split() for p in l: data_r.append(np.uint8(p)) # G for i in temp_g: if type(i) is int: data_g.append(np.uint8(i)) else: l = i.split() for p in l: data_g.append(np.uint8(p)) temp1 = np.asarray(data_b) temp2 = np.asarray(data_g) temp3 = np.asarray(data_r) img_B = temp1.reshape(height, width) img_G = temp2.reshape(height, width) img_R = temp3.reshape(height, width) img = cv.merge((img_B, img_G, img_R)) E_T = time.time() time_elapsed = (E_T - S_T) print("DONEEEEEEEEE\n\n\n") return time_elapsed