-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpegquality-compare.py
346 lines (295 loc) · 15 KB
/
jpegquality-compare.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
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
#! /usr/bin/env python3
"""
Estimate JPEG quality using ImageMagick heuristic, modified ImageMagick heuristic and
least squares matching method
"""
import math
import argparse
import csv
from PIL import Image
def parseCommandLine():
"""Parse command line"""
parser = argparse.ArgumentParser()
parser.add_argument('JPEGsIn',
action="store",
type=str,
nargs='+',
help="input JPEG(s) (wildcards allowed)")
parser.add_argument('--verbose',
action="store_true",
help="print variable values at each iteration",
dest="verboseFlag",
default=False)
# Parse arguments
args = parser.parse_args()
return args
def computeJPEGQuality_im_orig(image, verboseFlag):
"""Returns JPEG quality of a JPEG image based on original ImageMagick
algorithm"""
_HASH_2 = [ 1020, 1015, 932, 848, 780, 735, 702, 679, 660, 645,
632, 623, 613, 607, 600, 594, 589, 585, 581, 571,
555, 542, 529, 514, 494, 474, 457, 439, 424, 410,
397, 386, 373, 364, 351, 341, 334, 324, 317, 309,
299, 294, 287, 279, 274, 267, 262, 257, 251, 247,
243, 237, 232, 227, 222, 217, 213, 207, 202, 198,
192, 188, 183, 177, 173, 168, 163, 157, 153, 148,
143, 139, 132, 128, 125, 119, 115, 108, 104, 99,
94, 90, 84, 79, 74, 70, 64, 59, 55, 49,
45, 40, 34, 30, 25, 20, 15, 11, 6, 4,
0 ]
_SUMS_2 = [ 32640, 32635, 32266, 31495, 30665, 29804, 29146, 28599, 28104,
27670, 27225, 26725, 26210, 25716, 25240, 24789, 24373, 23946,
23572, 22846, 21801, 20842, 19949, 19121, 18386, 17651, 16998,
16349, 15800, 15247, 14783, 14321, 13859, 13535, 13081, 12702,
12423, 12056, 11779, 11513, 11135, 10955, 10676, 10392, 10208,
9928, 9747, 9564, 9369, 9193, 9017, 8822, 8639, 8458,
8270, 8084, 7896, 7710, 7527, 7347, 7156, 6977, 6788,
6607, 6422, 6236, 6054, 5867, 5684, 5495, 5305, 5128,
4945, 4751, 4638, 4442, 4248, 4065, 3888, 3698, 3509,
3326, 3139, 2957, 2775, 2586, 2405, 2216, 2037, 1846,
1666, 1483, 1297, 1109, 927, 735, 554, 375, 201,
128, 0 ]
_HASH_1 = [ 510, 505, 422, 380, 355, 338, 326, 318, 311, 305,
300, 297, 293, 291, 288, 286, 284, 283, 281, 280,
279, 278, 277, 273, 262, 251, 243, 233, 225, 218,
211, 205, 198, 193, 186, 181, 177, 172, 168, 164,
158, 156, 152, 148, 145, 142, 139, 136, 133, 131,
129, 126, 123, 120, 118, 115, 113, 110, 107, 105,
102, 100, 97, 94, 92, 89, 87, 83, 81, 79,
76, 74, 70, 68, 66, 63, 61, 57, 55, 52,
50, 48, 44, 42, 39, 37, 34, 31, 29, 26,
24, 21, 18, 16, 13, 11, 8, 6, 3, 2,
0 ]
_SUMS_1 = [ 16320, 16315, 15946, 15277, 14655, 14073, 13623, 13230, 12859,
12560, 12240, 11861, 11456, 11081, 10714, 10360, 10027, 9679,
9368, 9056, 8680, 8331, 7995, 7668, 7376, 7084, 6823,
6562, 6345, 6125, 5939, 5756, 5571, 5421, 5240, 5086,
4976, 4829, 4719, 4616, 4463, 4393, 4280, 4166, 4092,
3980, 3909, 3835, 3755, 3688, 3621, 3541, 3467, 3396,
3323, 3247, 3170, 3096, 3021, 2952, 2874, 2804, 2727,
2657, 2583, 2509, 2437, 2362, 2290, 2211, 2136, 2068,
1996, 1915, 1858, 1773, 1692, 1620, 1552, 1477, 1398,
1326, 1251, 1179, 1109, 1031, 961, 884, 814, 736,
667, 592, 518, 441, 369, 292, 221, 151, 86,
64, 0 ]
qsum = 0
qdict = image.quantization
for i, qtable in qdict.items():
qsum += sum(qtable)
if len(qdict) >= 1:
qvalue = qdict[0][2]+qdict[0][53]
hashes, sums = _HASH_1, _SUMS_1
if len(qdict) >= 2:
qvalue += qdict[1][0]+qdict[1][-1]
hashes, sums = _HASH_2, _SUMS_2
for i in range(100):
if verboseFlag:
print("i: {}, qvalue: {}, hashes[i]:{}, qsum:{}, sums[i]{}".format(i,qvalue, hashes[i], qsum, sums[i]))
if ((qvalue < hashes[i]) and (qsum < sums[i])):
continue
if (((qvalue <= hashes[i]) and (qsum <= sums[i])) or (i >= 50)):
return i+1
break
return -1
def computeJPEGQuality_im_mod(image, verboseFlag):
"""Returns JPEG quality and exactness flag of a JPEG image based on modified ImageMagick
algorithm that omits i >= 50 condition"""
_HASH_2 = [ 1020, 1015, 932, 848, 780, 735, 702, 679, 660, 645,
632, 623, 613, 607, 600, 594, 589, 585, 581, 571,
555, 542, 529, 514, 494, 474, 457, 439, 424, 410,
397, 386, 373, 364, 351, 341, 334, 324, 317, 309,
299, 294, 287, 279, 274, 267, 262, 257, 251, 247,
243, 237, 232, 227, 222, 217, 213, 207, 202, 198,
192, 188, 183, 177, 173, 168, 163, 157, 153, 148,
143, 139, 132, 128, 125, 119, 115, 108, 104, 99,
94, 90, 84, 79, 74, 70, 64, 59, 55, 49,
45, 40, 34, 30, 25, 20, 15, 11, 6, 4,
0 ]
_SUMS_2 = [ 32640, 32635, 32266, 31495, 30665, 29804, 29146, 28599, 28104,
27670, 27225, 26725, 26210, 25716, 25240, 24789, 24373, 23946,
23572, 22846, 21801, 20842, 19949, 19121, 18386, 17651, 16998,
16349, 15800, 15247, 14783, 14321, 13859, 13535, 13081, 12702,
12423, 12056, 11779, 11513, 11135, 10955, 10676, 10392, 10208,
9928, 9747, 9564, 9369, 9193, 9017, 8822, 8639, 8458,
8270, 8084, 7896, 7710, 7527, 7347, 7156, 6977, 6788,
6607, 6422, 6236, 6054, 5867, 5684, 5495, 5305, 5128,
4945, 4751, 4638, 4442, 4248, 4065, 3888, 3698, 3509,
3326, 3139, 2957, 2775, 2586, 2405, 2216, 2037, 1846,
1666, 1483, 1297, 1109, 927, 735, 554, 375, 201,
128, 0 ]
_HASH_1 = [ 510, 505, 422, 380, 355, 338, 326, 318, 311, 305,
300, 297, 293, 291, 288, 286, 284, 283, 281, 280,
279, 278, 277, 273, 262, 251, 243, 233, 225, 218,
211, 205, 198, 193, 186, 181, 177, 172, 168, 164,
158, 156, 152, 148, 145, 142, 139, 136, 133, 131,
129, 126, 123, 120, 118, 115, 113, 110, 107, 105,
102, 100, 97, 94, 92, 89, 87, 83, 81, 79,
76, 74, 70, 68, 66, 63, 61, 57, 55, 52,
50, 48, 44, 42, 39, 37, 34, 31, 29, 26,
24, 21, 18, 16, 13, 11, 8, 6, 3, 2,
0 ]
_SUMS_1 = [ 16320, 16315, 15946, 15277, 14655, 14073, 13623, 13230, 12859,
12560, 12240, 11861, 11456, 11081, 10714, 10360, 10027, 9679,
9368, 9056, 8680, 8331, 7995, 7668, 7376, 7084, 6823,
6562, 6345, 6125, 5939, 5756, 5571, 5421, 5240, 5086,
4976, 4829, 4719, 4616, 4463, 4393, 4280, 4166, 4092,
3980, 3909, 3835, 3755, 3688, 3621, 3541, 3467, 3396,
3323, 3247, 3170, 3096, 3021, 2952, 2874, 2804, 2727,
2657, 2583, 2509, 2437, 2362, 2290, 2211, 2136, 2068,
1996, 1915, 1858, 1773, 1692, 1620, 1552, 1477, 1398,
1326, 1251, 1179, 1109, 1031, 961, 884, 814, 736,
667, 592, 518, 441, 369, 292, 221, 151, 86,
64, 0 ]
qsum = 0
qdict = image.quantization
for i, qtable in qdict.items():
qsum += sum(qtable)
if len(qdict) >= 1:
qvalue = qdict[0][2]+qdict[0][53]
hashes, sums = _HASH_1, _SUMS_1
if len(qdict) >= 2:
qvalue += qdict[1][0]+qdict[1][-1]
hashes, sums = _HASH_2, _SUMS_2
for i in range(100):
if verboseFlag:
print("i: {}, qvalue: {}, hashes[i]:{}, qsum:{}, sums[i]{}".format(i, qvalue, hashes[i], qsum, sums[i]))
if ((qvalue < hashes[i]) and (qsum < sums[i])):
continue
else:
quality= i+1
exact = qsum <= sums[i]
return quality, exact
break
return -1, False
def computeJPEGQuality_lsm(image):
"""Estimates JPEG quality using least squares matching between image
quantization tables and standard tables from the JPEG ISO standard.
This compares the image quantization tables against the standard quantization
tables for *all* possible quality levels, which are generated using
Equations 1 and 2 in Kornblum (2008):
https://www.sciencedirect.com/science/article/pii/S1742287608000285
Returns quality estimate, root mean squared error of residuals between
image quantization coefficients and corresponding standard coefficients,
and Nash-Sutcliffe Efficiency measure.
"""
# Standard JPEG luminance and chrominance quantization tables
# for 50% quality (ISO/IEC 10918-1 : 1993(E)), Annex K)
lum_base = [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]
chrom_base = [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]
# Image quantization tables
qdict = image.quantization
noTables = len(qdict)
# Default quantization table bit depth
qBitDepth = 8
if max(qdict[0]) > 255:
# Any values greater than 255 indicate bir depth 16
qBitDepth = 16
if noTables >= 2:
if max(qdict[1]) > 255:
qBitDepth = 16
# Calculate mean of all value in quantization tables
Tsum = sum(qdict[0])
if noTables >= 2:
Tsum += sum(qdict[1])
Tmean = Tsum / (noTables*64)
# List for storing squared error values
errors = []
# List for storing Nash–Sutcliffe Efficiency values
nseVals = []
# Iterate over all quality levels
for i in range(100):
# Quality level
Q = i+1
# Scaling factor (Eq 1 in Kornblum, 2008)
if Q < 50:
S = 5000/Q
else:
S = 200 - 2*Q
# Initialize sum of squared differences between image quantization values
# and corresponding values from standard q tables for this quality level
sumSqErrors = 0
# Initialize sum of squared differences between image quantization values
# and mean image quantization value (needed to calculate Nash Efficiency)
sumSqMean = 0
# Iterate over all values in quantization tables for this quality
for j in range(64):
# Compute standard luminance table value from scaling factor
# (Eq 2 in Kornblum, 2008)
Tslum = max(math.floor((S*lum_base[j] + 50) / 100), 1)
# Cap Tslum at 255 if bit depth is 8
if qBitDepth == 8:
Tslum = min(Tslum, 255)
# Update sum of squared errors relative to corresponding
# image table value
sumSqErrors += (qdict[0][j] - Tslum)**2
# Sum of luminance and chrominance values
Tcombi = qdict[0][j]
if noTables >= 2:
# Compute standard chrominance table value from scaling factor
# (Eq 2 in Kornblum, 2008)
Tschrom = max(math.floor((S*chrom_base[j] + 50) / 100), 1)
# Cap Tschrom at 255 if bit depth is 8
if qBitDepth == 8:
Tschrom = min(Tschrom, 255)
# Update sum of squared errors relative to corresponding
# image table value
sumSqErrors += (qdict[1][j] - Tschrom)**2
# Update sum of luminance and chrominance values
Tcombi += qdict[1][j]
# Update sumSqMMean
sumSqMean += (Tcombi - Tmean)**2
j += 1
# Calculate Nash-Sutcliffe Effiency
nse = 1 - sumSqErrors/sumSqMean
# Add calculated statistics to lists
errors.append(sumSqErrors)
nseVals.append(nse)
# Quality is estimated as level with smallest sum of squared errors
# Note that this will return the smallest quality level in case
# the smallest SSE occurs for more than one level!
# TODO: perhaps add a check for this and report as output?
qualityEst = errors.index(min(errors)) + 1
# Corresponding SSE. Value 0 indicates exact match with standard JPEG
# quantization tables. Any other value means non-standard tables were
# used, and quality estimate is an approximation
sumSqErrors = min(errors)
# Compute corresponding root mean squared error
rmsError = round(math.sqrt(sumSqErrors / (noTables * 64)), 3)
nse = round(max(nseVals), 3)
return qualityEst, rmsError, nse
def main():
args = parseCommandLine()
myJPEGs = args.JPEGsIn
myJPEGs.sort()
verboseFlag = args.verboseFlag
fileOut = "jpeg-quality-comparison.csv"
resultList = [["file", "q_im_orig", "q_im_mod",
"exact_im_mod", "q_lsm", "rmse_lsm", "nse_lsm"]]
for JPEG in myJPEGs:
with open(JPEG, 'rb') as fIn:
im = Image.open(fIn)
im.load()
q_im_orig = computeJPEGQuality_im_orig(im, verboseFlag)
q_im_mod, exact_im_mod = computeJPEGQuality_im_mod(im, verboseFlag)
q_lsm, rmse_lsm, nse_lsm = computeJPEGQuality_lsm(im)
resultList.append([JPEG, q_im_orig, q_im_mod, exact_im_mod,
q_lsm, rmse_lsm, nse_lsm])
with open(fileOut, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(resultList)
if __name__ == "__main__":
main()