Finding the ideal image quality setting
Michael Seifert, 2018-12-14Anyone who has edited an image knows about the save dialog where the user is asked to choose a quality level. When saving JPEG files, the number varies between 1 and 100. The quality setting affects not only the visual quality of an image, but also the file size. Image size is important for high-ranking and user-friendly websites. At the same time, users want to see visually pleasing images. Finding the sweet spot between these two requirements is not an easy task, especially since the impact of changing the image quality is not predictable. This article gives a quick introduction to image quality assessment (IQA) and shows a method to automatically determine image quality settings using available tools.
The following slideshow depicts images and file sizes in varying quality settings:
You may find that the high-quality images look largely the same, although the file size is constantly decreasing. If the quality level drops too low, the images suffer from visible distortion. Let's have a look at how file size behaves based on the chosen quality level. For this purpose, I saved the above image in every quality level, measured its file size, and normalized it to the image resolution. The following figure shows the image size in bits per pixel for specific JPEG quality settings:
The graph suggests that file size increases exponentially with the JPEG quality level. The graph is also strictly monotonic, which means in this case that an increase of quality level always leads to an increase in file size. Changes in quality have the highest impact on file size between quality levels of 79 to 100. But how do we know at which point the quality level is sufficient and does not produce a distorted image?
Measuring image quality
In order measure the quality degradation caused by compression, we can compare and rate the original image and the compressed image.1 The most straightforward way to do so is to make a visual comparison. We rate both the original and the compressed image, for example on a scale from 1 to 100, and calculate the rating difference. This is a form of a subjective quality metric,2 because it varies between different human observers. A subjective quality assessment may be applicable for a couple of images, but not for larger numbers. Software engineers like to automate things and automation requires an objective quality metric.
Enter Structural Similarity (SSIM): Along with its variants, SSIM is a popular IQA metric. It tries to measure structural changes in an image rather than measuring deviations of individual pixels. The original paper defined SSIM only for gray-scale images.3 To keep things simple, I will be using DSSIM, a variant of SSIM that supports RGBA created by Kornel Lesiński. DSSIM measures the perceived difference between two images. Two images are identical when the value is 0 and increases with increasing difference between the images. Let's see how JPEG image quality influences the DSSIM rating in our specific example (lower ratings are better):
The DSSIM rating seems to be inversely exponential to JPEG image quality. The graph is also (strictly) monotonic so that an increase of quality level always leads to a decrease of DSSIM rating, i.e. a higher quality image.
Guiding the image encoding process
The DSSIM rating is yet another number: Similar to the quality level we cannot tell which DSSIM values are good enough for our purposes. However, a metric such as DSSIM has a couple of advantages. For one it tries to be independent of the image's content. Image encoders, on the contrary, may produce higher or lower quality images for different input content at the same encoding settings. For another, an objective metric can be used for different image encoders, whereas quality settings are specific to the respective encoders.
To guide the image encoding process, we define some threshold for the maximum allowed DSSIM rating and increase JPEG quality to the highest level that lies below the threshold. The easiest way is a brute-force approach: We simply try all possible quality levels and select the one closest below the threshold. However, we found that the DSSIM rating graph is monotonic, so we can be a bit smarter. Applying a binary search algorithm will lead to an optimal result in at most 7 iterations. It may also be a good idea to employ some kind of heuristic, which is what cjpeg-dssim does.
The threshold varies with the selected metric and the use case. Therefore, it should generally be determined via a controlled experiment. There are research and case studies on the web, however, that suggest thresholds for specific metrics and use cases. For example, research suggests that images with SSIM values around 0.95 cannot be discerned from the original.4 This lines up with the results of practitioners which ended up using JPEG quality levels between 80 and 85.
It is possible to extend this approach to more encoding parameters than just the quality setting. Such an approach can easily lead to many possible combinations of parameters that need to be checked. Others tackled the large search space using machine learning techniques.
Ameto currently uses the metric PSNR-HVS to guide encoding parameters. We have previously shown that Ameto achieves better results than conventional encoders.
There is a vast number of image quality metrics to choose from. Make sure that you adjust the presented approach to your needs and validate the selected threshold with a representative subset of your data.