I allow users to upload images. However, I want to keep JPEG quality not more than 90%. What I plan to do is to detect the current quality: - If less than 90% do nothing - If mo
If your jpeg was created using a straight scaling of the standard image quantization tables and the 0-100 quality was used based on the Independent JPEG Group's formula, then, assuming you have the luminance quantization tables in an array called quantization
(such as Python's PIL module provides in image.quantization[0]
), then the original value can be obtained via:
if quantization[58] <= 100:
originalQuality = int(100 - quantization[58] / 2)
else:
originalQuality = int(5000.0 / 2.5 / quantization[15])
Basically, the default luminance quantization value #15 is 40 and #58 is 100, so these make handy values to extract the results from. IJG scales values about 50 via 5000 / Q
and below 50 via 200 - 2 * Q
. If the quality setting is less than 8, this won't give decent results (if quantization[5] == 255
) -- in that case, perhaps use quantization table position #5
See https://tools.ietf.org/html/rfc2435#section-4.2.