Commit 410cc91f authored by Bryce Hepner's avatar Bryce Hepner

first_dict name changes, docstrings

parent e0758036
......@@ -3,10 +3,10 @@ def produce_error_array(encoded_string, list_dic, bins, use_diff):
"""
This function decodes the encoded_matrix.
Input:
A (3 X 3): system of equation
list_dic (num_dic + 1,): a list of huffman coding table
encoded_matrix (512, 640): encoded matrix
bins (num_bins - 1,): a list of threshold to cut the bins
use_diff (bool): whether or not to use the alternate differencing system
Return:
decode_matrix (512, 640): decoded matrix
......
This diff is collapsed.
......@@ -21,7 +21,7 @@ if __name__ == "__main__":
scenes = file_extractor(folder_name)
images = image_extractor(scenes)
dview = initialize()
list_dic = np.load("first_dic.npy", allow_pickle=True)
list_dic = np.load("first_dict.npy", allow_pickle=True)
bins = [21,32,48]
starttime = time()
......@@ -30,7 +30,7 @@ if __name__ == "__main__":
Takes in the filename and writes the compressed image
"""
list_dic = np.load("first_dic.npy", allow_pickle=True)
list_dic = np.load("first_dict.npy", allow_pickle=True)
bins = [21,32,48]
image, new_error, diff = huffman(filename, 4, False)
encoded_string = encoder(new_error, list_dic, diff, bins)
......@@ -42,7 +42,7 @@ if __name__ == "__main__":
with open(newname + "_Compressed.txt", 'wb') as f:
f.write(inletters)
def decode_an_image(filename):
list_dic = np.load("first_dic.npy", allow_pickle=True)
list_dic = np.load("first_dict.npy", allow_pickle=True)
bins = [21,32,48]
image, new_error, diff = huffman(filename, 4, False)
if filename[-5:] == ".tiff":
......
......@@ -65,14 +65,13 @@ def predict_pix(tiff_image_path, difference = True):
Input:
tiff_image_path (string): path to the tiff file
difference (bool): whether or not to use the alternate differencing system
Return:
image ndarray(512 X 640): original image
diff. ndarray(325380,): IF difference = TRUE, difference between the min and max of four neighbors exclude the boundary
ELSE: the residuals of the four nearest pixels to a fitted hyperplane
error ndarray(325380,): difference between the original image and predicted image
A ndarray(3 X 3): system of equation
"""
image_obj = Image.open(tiff_image_path) #Open the image and read it as an Image object
image_array = np.array(image_obj)[1:].astype(int) #Convert to an array, leaving out the first row because the first row is just housekeeping data
......@@ -187,7 +186,8 @@ def make_dictionary(tiff_image_path_list, num_bins=4, difference = True):
Input:
tiff_image_path (string): path to the tiff file
num_bins (int): number of bins
difference (bool): whether or not to use the alternate differencing system
Return:
huffman_encoding_list list (num_bins + 1): a list of dictionary, each dictionary is a huffman encoding
bins list (num_bins - 1,): a list of threshold to cut the bins
......@@ -278,16 +278,13 @@ def huffman(tiff_image_path, num_bins=4, difference = True):
Input:
tiff_image_path (string): path to the tiff file
num_bins (int): number of bins
difference (bool): whether or not to use the alternate differencing system
Return:
huffman_encoding_list list (num_bins + 1): a list of dictionary
image_as_array ndarray (512, 640): original image
new_error ndarray (512, 640): error that includes the boundary
diff ndarray (510, 638): difference of min and max of the 4 neighbors
boundary ndarray (2300,): the boundary values after subtracting the very first pixel value
predict ndarray (325380,): the list of predicted values
bins list (num_bins - 1,): a list of threshold to cut the bins
A ndarray (3 X 3): system of equation
"""
# get the image_as_array, etc
image_as_array, diff, error= predict_pix(tiff_image_path, difference)
......@@ -374,8 +371,9 @@ def encoder(error, list_dic, diff, bins):
bins (num_bins - 1,): a list of threshold to cut the bins
Return:
encoded (512, 640): encoded matrix
returnable_encode (512, 640): encoded matrix
"""
returnable_encode = ""
# copy the error matrix (including the boundary)
encoded = np.copy(error).astype(int).astype(str).astype(object)
......@@ -400,10 +398,10 @@ def decoder(encoded_string, list_dic, bins, use_diff):
"""
This function decodes the encoded_matrix.
Input:
A (3 X 3): system of equation
encoded_string (string): This is the input, the available information in 1s, 0s
list_dic (num_dic + 1,): a list of huffman coding table
encoded_matrix (512, 640): encoded matrix
bins (num_bins - 1,): a list of threshold to cut the bins
use_diff (bool): whether or not to use the alternate differencing system
Return:
decode_matrix (512, 640): decoded matrix
......@@ -483,10 +481,24 @@ def decoder(encoded_string, list_dic, bins, use_diff):
return decode_matrix.astype(int)
def read_from_file(filename):
'''
This function reads the file and return the content in a list
Input:
filename (string): the name of the file
Return:
content (list): the content of the file
'''
with open(filename, 'rb') as file:
return file.read()
def bitstring_to_bytes(input_string):
'''
This function converts the bitstring to bytes
Input:
input_string (string): the bitstring
Return:
output_string (string): the bytes
'''
int_array = []
length_of_string = len(input_string)
while length_of_string >= 8:
......@@ -501,12 +513,28 @@ def bitstring_to_bytes(input_string):
return bytes(int_array)
def bytes_to_bitstring(input_bytearray):
'''
This function converts the bytes to bitstring
Input:
input_bytearray (bytearray): the bytes
Return:
output_string (string): the bitstring
'''
end_string = ""
int_array = [i for i in input_bytearray]
for i, item in enumerate(int_array):
end_string += (bin(item)[2:].zfill(8))
return end_string
def text_to_tiff(filename, list_dic, bins):
'''
This function converts and saves the text to tiff
Input:
filename (string): the name of the file
list_dic (list): the list of dictionary
bins (list): the list of bins
Return:
None
'''
encoded_string = bytes_to_bitstring(read_from_file(filename))
reconstruct_image = decoder(encoded_string, list_dic, bins, False)
reconstruct_image = reconstruct_image.astype(np.uint16)
......@@ -521,7 +549,7 @@ if __name__ == "__main__":
list_dic, bins = make_dictionary(images, 4, False)
file_sizes_new = []
file_sizes_old = []
# list_dic = np.load("first_dic.npy", allow_pickle="TRUE")
# list_dic = np.load("first_dict.npy", allow_pickle="TRUE")
bins = [21,32,48]
np.save("first_dict.npy", list_dic)
for i in range(len(images)):
......@@ -544,9 +572,9 @@ if __name__ == "__main__":
print(file_sizes_new)
print(file_sizes_old)
print(np.sum(file_sizes_new)/np.sum(file_sizes_old))
file_sizes_new.append(os.path.getsize("first_dic.npy"))
file_sizes_new.append(os.path.getsize("first_dict.npy"))
print(np.sum(file_sizes_new)/np.sum(file_sizes_old))
# list_dic = np.load("first_dic.npy", allow_pickle="TRUE")
# list_dic = np.load("first_dict.npy", allow_pickle="TRUE")
bins = [21,32,48]
# for i,item in enumerate(newnamesforlater):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment