0%

jpeg彩色图片压缩python实现

代码:

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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#RGB彩色图片jpeg压缩实现
import cv2
import numpy as np
import base64
import math

'''
jpeg压缩函数
data:要压缩的灰度图像数据流
quality_scale控制压缩质量(1-99),默认为50,值越小图像约清晰
return:得到压缩后的图像数据,为FFD9开头的jpeg格式字符串
'''
def compress(img_data,quality_scale=50):
#获取图像数据流宽高
m_height,m_width,_=img_data.shape
m_YTable=np.zeros(64,dtype=int)
m_CbCrTable=np.zeros(64,dtype=int)
#标准亮度量化表
Luminance_Quantization_Table=np.array([16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68, 109, 103, 77,
24, 35, 55, 64, 81, 104, 113, 92,
49, 64, 78, 87, 103, 121, 120, 101,
72, 92, 95, 98, 112, 100, 103, 99],dtype=np.uint8)
#标准色度量化表
Chrominance_Quantization_Table=np.array([ 17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99],dtype=np.uint8)

ZigZag=np.array([0, 1, 5, 6,14,15,27,28,
2, 4, 7,13,16,26,29,42,
3, 8,12,17,25,30,41,43,
9,11,18,24,31,40,44,53,
10,19,23,32,39,45,52,54,
20,22,33,38,46,51,55,60,
21,34,37,47,50,56,59,61,
35,36,48,49,57,58,62,63])

Standard_DC_Luminance_NRCodes= [ 0, 0, 7, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ]
Standard_DC_Luminance_Values= [4, 5, 3, 2, 6, 1, 0, 7, 8, 9, 10, 11]

Standard_DC_Chrominance_NRCodes=[0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
Standard_DC_Chrominance_Values=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Standard_AC_Luminance_NRCodes=[0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d]
Standard_AC_Luminance_Values=[0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa]

Standard_AC_Chrominance_NRCodes=[0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77]

Standard_AC_Chrominance_Values=[0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa]
#将bgr三维数据 扁平化为一维
m_rgbBuffer=img_data.flatten(order='C')
# print(m_rgbBuffer[:10])
#初始化量化表
if quality_scale<=0:
quality_scale=1
elif quality_scale>=100:
quality_scale=99
for i in range(64):
tmp=int((Luminance_Quantization_Table[i]*quality_scale+50)/100)
if tmp<=0:
tmp=1
elif tmp>255:
tmp=255
m_YTable[ZigZag[i]]=tmp
tmp=int((Chrominance_Quantization_Table[i]*quality_scale+50)/100)
if tmp<=0:
tmp=1
elif tmp>255:
tmp=255
m_CbCrTable[ZigZag[i]]=tmp

# print(m_CbCrTable)
#初始化huffman表
m_Y_DC_Huffman_Table=np.zeros((12,2),dtype=int)
m_Y_AC_Huffman_Table=np.zeros((256,2),dtype=int)
m_CbCr_DC_Huffman_Table=np.zeros((12,2),dtype=int)
m_CbCr_AC_Huffman_Table=np.zeros((256,2),dtype=int)
_computeHuffmanTable(Standard_DC_Luminance_NRCodes, Standard_DC_Luminance_Values, m_Y_DC_Huffman_Table);
_computeHuffmanTable(Standard_AC_Luminance_NRCodes, Standard_AC_Luminance_Values, m_Y_AC_Huffman_Table);
_computeHuffmanTable(Standard_DC_Chrominance_NRCodes, Standard_DC_Chrominance_Values, m_CbCr_DC_Huffman_Table);
_computeHuffmanTable(Standard_AC_Chrominance_NRCodes, Standard_AC_Chrominance_Values, m_CbCr_AC_Huffman_Table);
# print(m_Y_AC_Huffman_Table[:12])

fp=open('out.jpg','wb')
#添加jpeg文件头
res=""
#SOI(文件头),共89个字节
res+='FFD8'
#APP0图像识别信息
res+='FFE000104A46494600010100000100010000'
#DQT定义量化表
res+='FFDB008400'
#64字节的量化表
for i in m_YTable:
res+=hex(i)[2:].rjust(2,'0')
res+='01'
for i in m_CbCrTable:
res+=hex(i)[2:].rjust(2,'0')

#SOF0图像基本信息,13个字节
res+='FFC0001108'
res+=hex(m_height)[2:].rjust(4,'0')
res+=hex(m_width)[2:].rjust(4,'0')
res+='03011100021101031101'
#DHT定义huffman表,33个字节+183
# res+='FFC4001F0000010501010101010100000000000000'
res+='FFC401A200'
for i in Standard_DC_Luminance_NRCodes:
res+=hex(i)[2:].rjust(2,'0')
for i in Standard_DC_Luminance_Values:
res+=hex(i)[2:].rjust(2,'0')
res+='10'
for i in Standard_AC_Luminance_NRCodes:
res+=hex(i)[2:].rjust(2,'0')
for i in Standard_AC_Luminance_Values:
res+=hex(i)[2:].rjust(2,'0')
res+='01'
for i in Standard_DC_Chrominance_NRCodes:
res+=hex(i)[2:].rjust(2,'0')
for i in Standard_DC_Chrominance_Values:
res+=hex(i)[2:].rjust(2,'0')
res+='11'
for i in Standard_AC_Chrominance_NRCodes:
res+=hex(i)[2:].rjust(2,'0')
for i in Standard_AC_Chrominance_Values:
res+=hex(i)[2:].rjust(2,'0')
# #SOS扫描行开始,10个字节
res+='FFDA000C03010002110311003f00'
fp.write(base64.b16decode(res.upper()))
prev_DC_Y=[0]
prev_DC_Cb=[0]
prev_DC_Cr=[0]
# prev_DC=[0,0,0]
newByte=[0]
newBytePos=[7]
for yPos in range(0,m_height,8):
for xPos in range(0,m_width,8):
#颜色空间转换后的数据
yData=np.zeros((64),dtype=int)
cbData=np.zeros((64),dtype=int)
crData=np.zeros((64),dtype=int)
#dct及量化后的数据
yQuant=np.zeros((64),dtype=int)
cbQuant=np.zeros((64),dtype=int)
crQuant=np.zeros((64),dtype=int)
#转换颜色空间 rgb转ycbcr
for y in range(8):
pos=(y+yPos)*m_width*3+xPos*3
for x in range(8):
B=m_rgbBuffer[pos]
pos+=1
G=m_rgbBuffer[pos]
pos+=1
R=m_rgbBuffer[pos]
pos+=1
yData[y*8+x] = (int)(0.299 * R + 0.587 * G + 0.114 * B - 128)
cbData[y*8+x] = (int)(-0.1687 * R - 0.3313 * G + 0.5 * B );
crData[y*8+x] = (int)(0.5 * R - 0.4187 * G - 0.0813 * B);
outputBitString=np.zeros((128,2),dtype=int)
bitStringCounts=[0];
#Y通道压缩
_foword_FDC(yData,yQuant,ZigZag,m_YTable)
_doHuffmanEncoding(yQuant,prev_DC_Y,m_Y_DC_Huffman_Table,m_Y_AC_Huffman_Table,outputBitString,bitStringCounts)
# print(m_Y_DC_Huffman_Table)
_write_bitstring_(outputBitString,bitStringCounts[0],newByte,newBytePos,fp)
# print(outputBitString)
#Cb通道压缩
_foword_FDC(cbData,cbQuant,ZigZag,m_CbCrTable);
_doHuffmanEncoding(cbQuant,prev_DC_Cb,m_CbCr_DC_Huffman_Table,m_CbCr_AC_Huffman_Table,outputBitString,bitStringCounts)
_write_bitstring_(outputBitString,bitStringCounts[0],newByte,newBytePos,fp)
# # print(m_CbCrTable)
# # print(cbQuant)
# # return
#Cr通道压缩
_foword_FDC(crData,crQuant,ZigZag,m_CbCrTable);
_doHuffmanEncoding(crQuant,prev_DC_Cr,m_CbCr_DC_Huffman_Table,m_CbCr_AC_Huffman_Table,outputBitString,bitStringCounts)
_write_bitstring_(outputBitString,bitStringCounts[0],newByte,newBytePos,fp)

# print(bitStringCounts[0])
# print(outputBitString)
# print(yQuant)
# return
fp.write(base64.b16decode('FFD9'))
fp.close()


# #转换颜色空间
# def _convertColorSpace(xPos,yPos,yData,cbData,crData):
# for y in range(8):
#把huffman编码后的结果写入文件
def _write_bitstring_(bs,counts,newByte,newBytePos,fp):
mask=[1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768]
for i in range(counts):
value=bs[i][1]
posval=bs[i][0]-1
# print(posval,value)
while(posval>=0):
if value & mask[posval] !=0:
newByte[0]=newByte[0] | mask[newBytePos[0]]
posval-=1
newBytePos[0]-=1
if newBytePos[0]<0:
#write to steam
fp.write(base64.b16decode(hex(newByte[0]).upper()[2:].rjust(2,'0')))
if newByte[0]==0xFF:
#Handle special case
fp.write(base64.b16decode('00'))
#Reinitialize
newBytePos[0]=7
newByte[0]=0

#dct离散余弦变换加量化
def _foword_FDC(channel_data,fdc_data,ZigZag,m_YTable):
PI=3.1415926
for v in range(8):
for u in range(8):
alpha_u = 1/math.sqrt(8.) if u==0 else 0.5
alpha_v = 1/math.sqrt(8.) if v==0 else 0.5
temp=0.
for x in range(8):
for y in range(8):
data=channel_data[y*8+x]
data*=np.cos((2*x+1)*u*PI/16.)
data*=np.cos((2*y+1)*v*PI/16.)
temp+=data
temp*=alpha_u*alpha_v/m_YTable[ZigZag[v*8+u]]
fdc_data[ZigZag[v*8+u]]=int((int)(temp+16384.5)-16384)

def _doHuffmanEncoding(DU,prevDC,HTDC,HTAC,outputBitString,bitStringCounts):
EOB=HTAC[0x00]
SIXTEEN_ZEROS=HTAC[0xF0]
index=0
#encode DC
dcDiff=(int)(DU[0]-prevDC[0])
prevDC[0]=DU[0]
if dcDiff==0:
outputBitString[index]=HTDC[0]
index+=1
else:
bs=_getBitCode(dcDiff)
outputBitString[index]=HTDC[bs[0]]
index+=1
outputBitString[index]=bs
index+=1
#encode ACs
endPos=63
#最后一个不为0的索引
while((endPos>0) and (DU[endPos]==0)):
endPos-=1
i=1
while(i<=endPos):

startPos=i
while((DU[i]==0) and (i<=endPos)):
i+=1
zeroCounts=i-startPos
if zeroCounts>=16:
for j in range(1,(int)(zeroCounts/16)+1):
outputBitString[index]=SIXTEEN_ZEROS
index+=1
zeroCounts=zeroCounts%16
bs=_getBitCode(DU[i])
outputBitString[index]=HTAC[(zeroCounts<<4)|bs[0]]
index+=1
outputBitString[index]=bs
index+=1
i+=1
if endPos!=63:
outputBitString[index]=EOB
index+=1
bitStringCounts[0]=index

def _getBitCode(value):
ret=np.zeros((2),dtype=int)
v=value if value>0 else -value
#bit的长度
length=0
while(v!=0):
v>>=1
length+=1
ret[1]=value if value>0 else ((1<<length)+value-1)
ret[0]=length
return ret



def _computeHuffmanTable(nr_codes,std_table,huffman_table):
pos_in_table=0;
code_value=0
for k in range(1,17):
for j in range(1,nr_codes[k-1]+1):
huffman_table[std_table[pos_in_table]][1]=code_value
huffman_table[std_table[pos_in_table]][0]=k;
pos_in_table+=1
code_value+=1
code_value<<=1

def main():
# img_path=sys.argv[1]
img_path='./lena.bmp'
# img_data=cv2.imread(img_path)[:,:,(2,1,0)]
#bgr顺序
img_data=cv2.imread(img_path)
compress(img_data)
# print(img_data)
# plt.imshow(img_data)
# plt.show()

if __name__=="__main__":
main()

------------- THE END! THANKS! -------------