Commit c6dfe9f2 authored by Bryce Hepner's avatar Bryce Hepner

new compression

parent c14dbb54
No preview for this file type
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
......@@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
......@@ -72,7 +72,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
......@@ -147,7 +147,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
......@@ -208,7 +208,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
......@@ -309,7 +309,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
......@@ -417,7 +417,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
......@@ -456,7 +456,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
......@@ -475,14 +475,18 @@
" input_string = input_string[8:]\n",
" length_of_string = len(input_string)\n",
" if length_of_string > 0:\n",
" int_array.append(int(input_string,2))\n",
" print(int_array[-12:])\n",
" zerobuffer = \"\"\n",
" for _ in range(8-length_of_string):\n",
" zerobuffer += \"0\"\n",
" int_array.append(int(input_string+zerobuffer,2))\n",
" # print(int_array[0:20])\n",
" # print(int_array[-12:])\n",
" return bytes(int_array)\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
......@@ -572,7 +576,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
......@@ -583,57 +587,42 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 106,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2080559\n",
"[189, 237, 174, 241, 149, 209, 142, 187, 71, 107, 178, 113]\n"
]
}
],
"outputs": [],
"source": [
"scenes = file_extractor()\n",
"newnamesforlater = []\n",
"images = image_extractor(scenes)\n",
"oglist_dic, ogbins = make_dictionary(images[:1], 4, False)\n",
"oglist_dic, ogbins = make_dictionary(images[:10], 4, False)\n",
"file_size_ratios = []\n",
"np.save(\"first_dic.npy\", oglist_dic)\n",
"for i in range(1):\n",
"for i in range(10):\n",
" list_dic, image, new_error, diff, bound, predict, bins = huffman(images[i], 4, False)\n",
" encoded_string1 = encoder(new_error, oglist_dic, diff, bound, ogbins)\n",
" # reconstruct_image = decoder(A, encoded_string, list_dic, bins, False)\n",
" # print(np.allclose(image, reconstruct_image))\n",
" print(len(encoded_string1))\n",
" inletters = bitstring_to_bytes(encoded_string1)\n",
"\n",
" # reconstruct_image = decoder(encoded_string, oglist_dic, ogbins, False)\n",
" # print(np.allclose(image, reconstruct_image))\n",
" # print(len(list_dic))\n",
" if images[0][:-5] == \".tiff\":\n",
" newname = images[0][:-5]\n",
" if images[i][:-5] == \".tiff\":\n",
" newname = images[i][:-5]\n",
" else:\n",
" newname = images[0][:-4]\n",
"\n",
" # with open(newname + \"_Compressed.txt\", 'wb') as f:\n",
" with open(\"MatrixNowString.txt\", 'wb') as f:\n",
" \n",
" newname = images[i][:-4]\n",
" newnamesforlater.append(newname + \"_Compressed.txt\")\n",
" with open(newname + \"_Compressed.txt\", 'wb') as f:\n",
" f.write(inletters)\n",
" file_size_ratios.append((os.path.getsize('MatrixNowString.txt'))/os.path.getsize('images/1626032610_393963/1626032610_393963_0.tiff'))"
" file_size_ratios.append((os.path.getsize(newname + \"_Compressed.txt\"))/os.path.getsize('images/1626032610_393963/1626032610_393963_0.tiff'))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 107,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.39572609126335023\n"
"0.3953878365239038\n"
]
}
],
......@@ -643,23 +632,10 @@
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6058229115470704"
]
},
"execution_count": 91,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0.6058229115470704"
]
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
......@@ -680,55 +656,32 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"0b100\n"
]
}
],
"source": [
"print(int(\"100\",2))\n",
"print(bin(4))"
]
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'\\xe4\\x05%4E\\x1a\\xd3 h\\xb1\\xb5\\x982\\xc9\\x0e\\xbc\\x83\\x90\\x101\\xd23\\xd9\\x16>'\n",
"1850876\n"
]
}
],
"outputs": [],
"source": [
"def bytes_to_bitstring(input_bytearray):\n",
" end_string = \"\"\n",
" print(input_bytearray[0:25])\n",
" # print(bytearray(input_bytearray,encoding=\"ISO-8859-1\").decode(\"utf-8\")[0:4])\n",
" int_array = [i for i in input_bytearray]\n",
" for item in int_array:\n",
" end_string += (bin(item)[2:])\n",
" for i, item in enumerate(int_array):\n",
" end_string += (bin(item)[2:].zfill(8))\n",
" return end_string\n",
"# oglist_dic = np.load(\"first_dic.npy\", allow_pickle=\"TRUE\")\n",
"oglist_dic = np.load(\"first_dic.npy\", allow_pickle=\"TRUE\")\n",
"\n",
"\n",
"ogbins = [12,60,180]\n",
"encoded_string2 = bytes_to_bitstring(read_from_file(\"images/1626033496_437803/1626033496_437803_11._Compressed.txt\"))\n",
"\n",
"print(len(encoded_string2))\n",
"reconstruct_image = decoder(encoded_string2, oglist_dic, ogbins, False)\n",
"# print(np.allclose(image, reconstruct_image))"
"for i,item in enumerate(newnamesforlater[0:10]):\n",
" list_dic, image, new_error, diff, bound, predict, bins = huffman(images[i], 4, False)\n",
" encoded_string2 = bytes_to_bitstring(read_from_file(item))\n",
" reconstruct_image = decoder(encoded_string2, oglist_dic, ogbins, False)\n",
" print(np.allclose(image, reconstruct_image))"
]
},
{
......
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