-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decode.py
126 lines (109 loc) · 3.61 KB
/
Decode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import time
import zlib
import numpy as np
from system import util
from PIL import Image, ImageFilter, ImageEnhance
###########################
### BEGIN OF PARAMETERS ###
# Block count in width
# Default value: 118
w_blocks = 118
# Block count in height
# Default value: 169
h_blocks = 169
# Gaussian blur level
# Default value: 1
lvl_blur = 1
# Gamma gain level
# Default value: 2
lvl_gamma = 2
### END OF PARAMETERS ###
###########################
def init(w_bl, h_bl, blur, gamma):
input_dir = './In-Image/'
output_dir = './Out-Data/'
util.print_header()
time_init = time.time()
input_file_name = util.get_input_file(input_dir)
img = apply_gamma(apply_blur(util.load_image(input_dir + input_file_name, True), blur), gamma)
ranges = calc_ranges(img.size, w_bl, h_bl)
export_data(zlib_decompress(int_convert(octal_convert(read_pixels(img, ranges)))), output_dir)
print(f'Done in {round(time.time() - time_init, 3)}s.')
util.full_exit(0)
def apply_blur(img, lvl):
print('[INFO] Applying Gaussian blur...')
img = img.filter(ImageFilter.GaussianBlur(lvl))
print('[INFO] Gaussian blur applied!')
return img
def apply_gamma(img, lvl):
print('[INFO] Applying gamma enhance...')
converter = ImageEnhance.Color(img)
img = converter.enhance(lvl)
print('[INFO] Gamma enhance applied!')
return img
def calc_ranges(img_size, w_blocks, h_blocks):
print('[INFO] Calculating block ranges...')
width, height = img_size
y_ranges = np.linspace(0, height, h_blocks + 1)
x_ranges = np.linspace(0, width, w_blocks + 1)
y_ranges = np.round(y_ranges[:-1] + np.diff(y_ranges) / 2)
x_ranges = np.round(x_ranges[:-1] + np.diff(x_ranges) / 2)
print('[INFO] Block ranges calculated!')
return (x_ranges, y_ranges)
def read_pixels(img, ranges):
print('[INFO] Reading pixels...')
pixels = []
for y in ranges[1]:
for x in ranges[0]:
pixels.append(img.getpixel((x, y)))
print('[INFO] All pixels read!')
return pixels
def octal_convert(pixels):
print('[INFO] Converting data to octal...')
octal = ''
colors = pixels[:8]
try:
for i in range(8, len(pixels) - 8):
octal = ''.join([octal, str(find_closest(colors, pixels[i]))])
except:
print('[ERROR] Octal conversion failed!')
util.full_exit(1)
print('[INFO] Data converted!')
return octal
def int_convert(octal):
print('[INFO] Converting data to integers...')
try:
ints = [int(octal[i:i + 3], 8) for i in range(0, len(octal), 3)]
except:
print('[ERROR] Integer conversion failed!')
util.full_exit(1)
print('[INFO] Data converted!')
return ints
def zlib_decompress(ints):
print('[INFO] Decompressing ZLib...')
data = b''
d = zlib.decompressobj()
try:
for i in range(len(ints)):
data = b''.join([data, d.decompress(d.unconsumed_tail + bytes([ints[i]]))])
if d.eof:
break
except:
print('[ERROR] ZLib decompression failed!')
util.full_exit(1)
print('[INFO] Data decompressed!')
return data
def export_data(data, out_dir):
print('[INFO] Exporting data...')
data = data.split(b'/', 1)
path = out_dir + data[0].decode()
with open(path, 'wb') as out:
out.write(data[1])
print(f'[INFO] File saved: {path}!')
def find_closest(colors, color):
colors = np.array(colors)
color = np.array(color)
distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))
return np.where(distances == np.amin(distances))[0][0]
if __name__ == '__main__':
init(w_blocks, h_blocks, lvl_blur, lvl_gamma)