Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
image-compression
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Elphel
image-compression
Commits
410cc91f
Project 'Elphel/master' was moved to 'Elphel/image-compression'. Please update any links and bookmarks that may still have the old path.
Commit
410cc91f
authored
Jul 13, 2022
by
Bryce Hepner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first_dict name changes, docstrings
parent
e0758036
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
312 deletions
+69
-312
ErrorImageCreator.py
ErrorImageCreator.py
+1
-1
FullTester.ipynb
FullTester.ipynb
+22
-293
RunningInParallel.py
RunningInParallel.py
+3
-3
WorkingPyDemo.py
WorkingPyDemo.py
+43
-15
No files found.
ErrorImageCreator.py
View file @
410cc91f
...
...
@@ -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
...
...
FullTester.ipynb
View file @
410cc91f
This diff is collapsed.
Click to expand it.
RunningInParallel.py
View file @
410cc91f
...
...
@@ -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_dic
t
.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_dic
t
.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_dic
t
.npy"
,
allow_pickle
=
True
)
bins
=
[
21
,
32
,
48
]
image
,
new_error
,
diff
=
huffman
(
filename
,
4
,
False
)
if
filename
[
-
5
:]
==
".tiff"
:
...
...
WorkingPyDemo.py
View file @
410cc91f
...
...
@@ -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_dic
t
.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_dic
t
.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_dic
t
.npy", allow_pickle="TRUE")
bins
=
[
21
,
32
,
48
]
# for i,item in enumerate(newnamesforlater):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment