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
......
......@@ -15,16 +15,16 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"521\n",
"263\n",
"0.4041653243824837\n"
"263\n",
"0.4041666246254766\n"
]
}
],
......@@ -35,7 +35,7 @@
"newnamesforlater = []\n",
"file_sizes_new = []\n",
"file_sizes_old = []\n",
"list_dic = np.load(\"first_dic.npy\", allow_pickle=\"TRUE\")\n",
"list_dic = np.load(\"first_dict.npy\", allow_pickle=\"TRUE\")\n",
"bins = [21,32,48]\n",
"for i in range(len(images)):\n",
" \n",
......@@ -50,14 +50,14 @@
" file_sizes_new.append((os.path.getsize(newname + \"_Compressed.txt\")))\n",
" file_sizes_old.append((os.path.getsize(newname + \"RemovedFirstLine\" + \".tif\")))\n",
" # file_sizes_old.append((os.path.getsize(newname + \".tiff\")))\n",
"file_sizes_new.append(os.path.getsize(\"first_dic.npy\"))\n",
"file_sizes_new.append(os.path.getsize(\"first_dict.npy\"))\n",
"print(len(newnamesforlater))\n",
"print(np.sum(file_sizes_new)/np.sum(file_sizes_old))\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
......@@ -71,7 +71,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"outputs": [
{
......@@ -80,12 +80,12 @@
"text": [
"0.3767853274384346\n",
"0.4979190885485795\n",
"0.3972905625173536\n",
"0.41933998568991976\n",
"0.40415934529931374\n",
"0.4028230157727312\n",
"0.39809636267662574\n",
"0.750887133437684\n"
"0.39729075321671686\n",
"0.4193614393682817\n",
"0.4041666246254766\n",
"0.4028289866441236\n",
"0.3980978882715315\n",
"0.7512288666965683\n"
]
}
],
......@@ -102,14 +102,14 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.5066909897511674\n",
"0.6097255182418949\n",
"0.6485158723863795\n"
]
}
......@@ -119,13 +119,13 @@
"for i, filename in enumerate(images):\n",
" newimage = Image.open(filename)\n",
" newimage = np.array(newimage)\n",
" newimage = newimage[1:]\n",
" newimage = newimage[1:].astype(np.uint16)\n",
" with open(newnamesforlater[i][:-4] + \".png\", 'wb') as f:\n",
" writer = png.Writer(newimage.shape[1], newimage.shape[0], greyscale=True, bitdepth=16)\n",
" writer.write(f, newimage)\n",
" pngsizes.append(os.path.getsize(newnamesforlater[i][:-4] + \".png\"))\n",
"print(np.sum(pngsizes)/np.sum(file_sizes_old))\n",
"print(0.6485158723863795)"
"# print(0.6485158723863795)"
]
},
{
......@@ -242,284 +242,14 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
"float32\n",
">u2\n",
"float32\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n",
">u2\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/home/bryce/git/master/FullTester.ipynb Cell 13'\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/bryce/git/master/FullTester.ipynb#ch0000011?line=5'>6</a>\u001b[0m \u001b[39mprint\u001b[39m(newimage\u001b[39m.\u001b[39mdtype)\n\u001b[1;32m <a href='vscode-notebook-cell:/home/bryce/git/master/FullTester.ipynb#ch0000011?line=6'>7</a>\u001b[0m newimage \u001b[39m=\u001b[39m Image\u001b[39m.\u001b[39mfromarray(newimage)\n\u001b[0;32m----> <a href='vscode-notebook-cell:/home/bryce/git/master/FullTester.ipynb#ch0000011?line=7'>8</a>\u001b[0m newimage\u001b[39m.\u001b[39;49msave(newnamesforlater[i][:\u001b[39m-\u001b[39;49m\u001b[39m4\u001b[39;49m]\u001b[39m+\u001b[39;49m \u001b[39m\"\u001b[39;49m\u001b[39mlzw\u001b[39;49m\u001b[39m\"\u001b[39;49m \u001b[39m+\u001b[39;49m \u001b[39m\"\u001b[39;49m\u001b[39m.tiff\u001b[39;49m\u001b[39m\"\u001b[39;49m, compression\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39mtiff_lzw\u001b[39;49m\u001b[39m'\u001b[39;49m, tiffinfo\u001b[39m=\u001b[39;49m{\u001b[39m317\u001b[39;49m: \u001b[39m2\u001b[39;49m})\n\u001b[1;32m <a href='vscode-notebook-cell:/home/bryce/git/master/FullTester.ipynb#ch0000011?line=9'>10</a>\u001b[0m lwz_sizes\u001b[39m.\u001b[39mappend(os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39mgetsize(newnamesforlater[i][:\u001b[39m-\u001b[39m\u001b[39m4\u001b[39m]\u001b[39m+\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mlzw\u001b[39m\u001b[39m\"\u001b[39m \u001b[39m+\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m.tiff\u001b[39m\u001b[39m\"\u001b[39m))\n\u001b[1;32m <a href='vscode-notebook-cell:/home/bryce/git/master/FullTester.ipynb#ch0000011?line=10'>11</a>\u001b[0m \u001b[39mprint\u001b[39m(np\u001b[39m.\u001b[39msum(lwz_sizes)\u001b[39m/\u001b[39mnp\u001b[39m.\u001b[39msum(file_sizes_old))\n",
"File \u001b[0;32m~/.local/lib/python3.8/site-packages/PIL/Image.py:2300\u001b[0m, in \u001b[0;36mImage.save\u001b[0;34m(self, fp, format, **params)\u001b[0m\n\u001b[1;32m <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/Image.py?line=2296'>2297</a>\u001b[0m fp \u001b[39m=\u001b[39m builtins\u001b[39m.\u001b[39mopen(filename, \u001b[39m\"\u001b[39m\u001b[39mw+b\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/Image.py?line=2298'>2299</a>\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m-> <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/Image.py?line=2299'>2300</a>\u001b[0m save_handler(\u001b[39mself\u001b[39;49m, fp, filename)\n\u001b[1;32m <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/Image.py?line=2300'>2301</a>\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mException\u001b[39;00m:\n\u001b[1;32m <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/Image.py?line=2301'>2302</a>\u001b[0m \u001b[39mif\u001b[39;00m open_fp:\n",
"File \u001b[0;32m~/.local/lib/python3.8/site-packages/PIL/TiffImagePlugin.py:1814\u001b[0m, in \u001b[0;36m_save\u001b[0;34m(im, fp, filename)\u001b[0m\n\u001b[1;32m <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/TiffImagePlugin.py?line=1810'>1811</a>\u001b[0m e\u001b[39m.\u001b[39msetimage(im\u001b[39m.\u001b[39mim, (\u001b[39m0\u001b[39m, \u001b[39m0\u001b[39m) \u001b[39m+\u001b[39m im\u001b[39m.\u001b[39msize)\n\u001b[1;32m <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/TiffImagePlugin.py?line=1811'>1812</a>\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[1;32m <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/TiffImagePlugin.py?line=1812'>1813</a>\u001b[0m \u001b[39m# undone, change to self.decodermaxblock:\u001b[39;00m\n\u001b[0;32m-> <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/TiffImagePlugin.py?line=1813'>1814</a>\u001b[0m l, s, d \u001b[39m=\u001b[39m e\u001b[39m.\u001b[39;49mencode(\u001b[39m16\u001b[39;49m \u001b[39m*\u001b[39;49m \u001b[39m1024\u001b[39;49m)\n\u001b[1;32m <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/TiffImagePlugin.py?line=1814'>1815</a>\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m _fp:\n\u001b[1;32m <a href='file:///home/bryce/.local/lib/python3.8/site-packages/PIL/TiffImagePlugin.py?line=1815'>1816</a>\u001b[0m fp\u001b[39m.\u001b[39mwrite(d)\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
"0.6169964597214944\n"
]
}
],
......@@ -528,8 +258,7 @@
"for i, filename in enumerate(images):\n",
" newimage = Image.open(filename)\n",
" newimage = np.array(newimage)\n",
" newimage = newimage[1:]\n",
" print(newimage.dtype)\n",
" newimage = newimage[1:].astype(np.uint16)\n",
" newimage = Image.fromarray(newimage)\n",
" newimage.save(newnamesforlater[i][:-4]+ \"lzw\" + \".tiff\", compression='tiff_lzw', tiffinfo={317: 2})\n",
"\n",
......@@ -601,14 +330,14 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"521\n",
"526\n",
"263\n"
]
}
......
......@@ -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