Commit c6dfe9f2 authored by Bryce Hepner's avatar Bryce Hepner

new compression

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