-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathencode.py
191 lines (150 loc) · 5.9 KB
/
encode.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# # --------------------------------------
# # @authur = "tangxi.zq"
# # @time = "2019-05-17"
# # @file = "encode.py"
# # Description :jpeg图像编码.
# # --------------------------------------
import argparse
import os
import math
import numpy as np
from utils import *
from scipy import fftpack
from PIL import Image
from Huffman import HuffmanTree
def quantize(block, component):
q = load_quantization_table(component)
return (block / q).round().astype(np.int32)
def block_to_zigzag(block):
return np.array([block[point] for point in zigzag_points(*block.shape)])
def dct_2d(image):
return fftpack.dct(fftpack.dct(image.T, norm='ortho').T, norm='ortho')
def run_length_encode(arr):
# 找到最后一个不为0的交流系数index
last_nonzero = -1
for i, elem in enumerate(arr):
if elem != 0:
last_nonzero = i
# 交流系数的中间符号
symbols = []
values = []
run_length = 0
for i, elem in enumerate(arr):
if i > last_nonzero:
symbols.append((0, 0))
values.append(int_to_binstr(0))
break
elif elem == 0 and run_length < 15:
run_length += 1
else:
size = bits_required(elem)
symbols.append((run_length, size))
values.append(int_to_binstr(elem))
run_length = 0
return symbols, values
def write_to_file(filepath, dc, ac, blocks_count, tables):
try:
f = open(filepath, 'w')
except FileNotFoundError as e:
raise FileNotFoundError(
"No such directory: {}".format(
os.path.dirname(filepath))) from e
for table_name in ['dc_y', 'ac_y', 'dc_c', 'ac_c']:
# 16 bits for 'table_size'
f.write(uint_to_binstr(len(tables[table_name]), 16))
for key, value in tables[table_name].items():
if table_name in {'dc_y', 'dc_c'}:
# 4 bits for the 'category'
# 4 bits for 'code_length'
# 'code_length' bits for 'huffman_code'
f.write(uint_to_binstr(key, 4))
f.write(uint_to_binstr(len(value), 4))
f.write(value)
else:
# 4 bits for 'run_length'
# 4 bits for 'size'
# 8 bits for 'code_length'
# 'code_length' bits for 'huffman_code'
f.write(uint_to_binstr(key[0], 4))
f.write(uint_to_binstr(key[1], 4))
f.write(uint_to_binstr(len(value), 8))
f.write(value)
# 32 bits for 'blocks_count'
f.write(uint_to_binstr(blocks_count, 32))
for b in range(blocks_count):
for c in range(3):
category = bits_required(dc[b, c])
symbols, values = run_length_encode(ac[b, :, c])
dc_table = tables['dc_y'] if c == 0 else tables['dc_c']
ac_table = tables['ac_y'] if c == 0 else tables['ac_c']
f.write(dc_table[category])
f.write(int_to_binstr(dc[b, c]))
for i in range(len(symbols)):
f.write(ac_table[tuple(symbols[i])])
f.write(values[i])
f.close()
def main():
'''
jpeg图像压缩过程实现
1. 将原始图像转换到YCrCb空间
2. DCT变换
3. 量化
4. 编码
'''
#载入参数
parser = argparse.ArgumentParser()
parser.add_argument("--input", help="path to the input image")
parser.add_argument("--output", help="path to the output image")
args = parser.parse_args()
input_file = args.input
output_file = args.output
#读取图像并转换到ycrcb空间
image = Image.open(input_file)
ycbcr = image.convert('YCbCr')
npmat = np.array(ycbcr, dtype=np.uint8)
rows, cols = npmat.shape[0], npmat.shape[1]
# dct分块:8*8
if rows % 8 == cols % 8 == 0:
blocks_count = rows // 8 * cols // 8
else:
#如果图像分辨率不能被8整除,将其补零成能被整除的
dct_rows = rows + 8 - rows % 8
dct_cols = cols + 8 - cols % 8
diff_cols = dct_cols - cols
diff_rows = dct_rows - rows
npmat = np.pad(npmat,((0,diff_rows), (0,diff_cols),(0,0)), mode = 'mean')
blocks_count = (dct_rows // 8)*(dct_cols // 8)
# raise ValueError(("the width and height of the image should both be mutiples of 8"))
# dc系数只有左上角一个,ac系数是剩下的63个
dc = np.empty((blocks_count, 3), dtype=np.int32)
ac = np.empty((blocks_count, 63, 3), dtype=np.int32)
for i in range(0, rows, 8):
for j in range(0, cols, 8):
try:
block_index += 1
except NameError:
block_index = 0
for k in range(3):
# 将像素值归一化在-127到128之间
block = npmat[i:i+8, j:j+8, k] - 128
dct_matrix = dct_2d(block)
quant_matrix = quantize(dct_matrix,'lum' if k == 0 else 'chrom')
zz = block_to_zigzag(quant_matrix)
dc[block_index, k] = zz[0]
ac[block_index, :, k] = zz[1:]
#Huffman编码,其中交流系数还需要游程编码
print(len(dc[:,0]))
print(dc.shape)
H_DC_Y = HuffmanTree(np.vectorize(bits_required)(dc[:, 0]))
H_DC_C = HuffmanTree(np.vectorize(bits_required)(dc[:, 1:].flat))
H_AC_Y = HuffmanTree(flatten(run_length_encode(ac[i, :, 0])[0] for i in range(blocks_count)))
H_AC_C = HuffmanTree(flatten(run_length_encode(ac[i, :, j])[0] for i in range(blocks_count) for j in [1, 2]))
tables = {'dc_y': H_DC_Y.value_to_bitstring_table(),
'ac_y': H_AC_Y.value_to_bitstring_table(),
'dc_c': H_DC_C.value_to_bitstring_table(),
'ac_c': H_AC_C.value_to_bitstring_table()}
write_to_file(output_file, dc, ac, blocks_count, tables)
if __name__ == "__main__":
print("start...")
main()
print("end...")