Compress image files with Guetzli

Small Wonders

© Lead Image © Stuart Monk, Fotolia.com

© Lead Image © Stuart Monk, Fotolia.com

Author(s):

The Guetzli image optimizer by Google developers produces smaller images than JPEG while maintaining the same quality, but it requires a powerful computer with a large working memory.

Images play an important role, especially on the Internet. Web hosting providers and users have a keen interest in efficient use of storage space and reduction of load times. With a need to budget its resources wisely, Google therefore set its developers the task of compressing image data.

The result is the Guetzli project – Guetzli being the name for a candy or cookie in the Alemannic German dialect. Under certain, frequently occurring conditions, Guetzli can reduce JPEG images by about a third compared with previous methods. Smaller file sizes then automatically result in quicker website load times.

The Chronicles of JPEG

For a long time, JPEG has been the trusted standard for image compression, but a closer look reveals that the algorithm sometimes produce rather unpleasant artifacts that are difficult to remove. Lossy algorithms remove details from images that are barely visible to the human eye to create larger uniform structures that can then be better compressed, whereas the lossless algorithms, such as JPEG2000 or JPEG-LS [1] generate larger image files. Some variants of Libjpeg [2] support the lossless method, so you should be able to use them under Linux with the appropriate software.

Dropbox made the first advance in JPEG algorithms by introducing Lepton [3], the image compressor, which retroactively compresses JPEGs. However, hardly any program can display the resulting images; to display them, you first have to convert back to the original JPEG. Gimp also can optimize JPEGs (see the "Optimizing JPEGs in Gimp" box). Although it is a fairly lengthy manual process, Gimp's scripting additions can automate the process.

Optimizing JPEGs in Gimp

When exporting images to the JPEG format in Gimp (File | Export As), under the Select File Type (By Extension) drop-down (Figure 1), select JPEG image or, if you have installed the corresponding plugin, JP2 image.

Figure 1: Gimp can store JPEG images in different variants.

In the following Export Image as JPEG dialog, the best approach is to choose Show preview in image window so that Gimp computes the file size of the exported images (Figure 2). It takes a bit of time for larger images, but it can help you find optimum parameters. Gimp displays several coding options under the Advanced Options drop-down.

Figure 2: The Advanced Options section manages many details when you export JPEG images.

In addition to the Quality slider, the most important correcting variables are Subsampling and Smoothing. Quality is a fairly intuitive parameter: How far you can reduce it largely depends on the specific image. Smoothing causes moderate softening that is often pleasing to the human eye.

Under Subsampling, you can directly tweak the algorithm and define the quality of the results. Better quality with the 4:4:4 (best quality) option costs computing time and results in larger files. Computing time can be influenced by the DCT method, as well: Fast Integers is the quickest option, with only minor quality loss.

Command-Line Goodies

The guetzli command implements the method from the command line. Calling the program without parameters shows the few, extremely simple options (Listing 1).

Listing 1

Guetzli Options

 

The --quality switch is the most important parameter. The developers have tried to maintain compatibility with the quality parameters used by Libjpeg (the most common JPEG converter) to ensure compatibility. To avoid visible artifacts, Guetzli only allows values of --quality 84 or higher.

Practical use of Guetzli is extremely simple. Calling Guetzli with the following pattern converts an image into a Guetzli JPEG:

$ guetzli --quality <nn> <input> <output>.jpg

Because Guetzli completely loads images for editing into main memory, you can also overwrite the input file, if necessary:

guetzli bild.jpg bild.jpg

The program does not output any warnings.

Input files can only be JPEG or PNG formats; any other format will result in the Error reading JPG data from input file error message. Guetzli expects images with the sRGB color profile and a gamma of 2.2 – a common default for cameras. The tool ignores any other color profile, so you have to convert the output image, if necessary, to prevent incorrect colors. For a PNG with an alpha channel, Guetzli converts the transparent areas to black.

As well as Guetzli works, it has some downsides: First, it requires significantly more computing power than the algorithms provided by Libjpeg. Second, the RAM requirement is unusually high, at around 300MB per megapixel. Thorough analysis costs time and space. Third, you might have to build Guetzli yourself (see the "Build Your Own Guetzli" box).

Build Your Own Guetzli

Not all common distributions have Guetzli in their package sources. You will find the latest version on GitHub [4], where the developers are constantly working on the code. Recently, they reduced memory usage by 16 bytes per pixel – quite important in light of Guetzli's high memory requirements. Arch Linux recommends installing the Git version in the AUR repository.

A Guetzli dependency is the current version of libpng-devel. However, when building, the developers do not rely on known methods; instead, they use Bazel [5], which is rarely used under Linux and might initially require some installation work. Subsequently, the

bazel build -c opt //:guetzli

command is sufficient. The source code provides the required "recipe."

Psychovisual Model

It seems amazing that there are still ways to improve such well-established methods as JPEG coding. All JPEG algorithms have ways to reduce data and use a (run-time) compression method to filter out and remove parts of the image that are not visible to the human eye.

Guetzli uses a new "psychovisual model," as the developers explain: "Guetzli tries to balance minimum loss and file size by using a search algorithm that seeks to overcome the difference between the psychovisual modeling of the JPEG format and the psychovisual model of Guetzli." Guetzli's algorithm, referred to as a "human visual processing system model" for the detection of essential components of images is based on a method called Butteraugli, which is also available as a separate tool [6].

The special trick of the applied Guetzli method lies in the visual masking of image information by a nearby second (strong) stimulus. In some aspects, it is similar to the Blur mask, the best of Gimp's sharpening filters. In this case, the algorithm accentuates an edge in the image by a second edge in the immediate vicinity. The Blur mask and Guetzli are oriented toward human visual perception and do not necessarily generate mathematically accurate results.

Because the models used to detect relevant image information differ, even with JPEG converters, the algorithms based on the models also differ and work effectively in different ways. It is useful to remove the "correct" parts of images to obtain a small file size and good visual quality.

The relatively universal methods based on visual perception are referred to as being "perceptual." However, they differ in many details. You will find detailed information about the Guetzli algorithm in the Google developer's blog [7], where the programmers show by example that environmental artifacts (ringing artifacts) in Guetzli are fewer than in other algorithms.

The developers regularly experiment with several images to check how well the algorithm works. They report a test in which more than 75% of the subjects rated the images generated with the Guetzli algorithm better than those produced by Libjpeg. However, occasional reports from users criticize the quality of Guetzli images, but these statements are not very convincing because the images that were used are often missing and the criticism therefore cannot be understood.

However, Guetzli goes one step further when compressing the data with a "quantization stage of compression" method that reduces the volume of data [8]. In recent years, this method has been developed and optimized continuously by chief developer Jyrki Alakuijala, which has led to a whole series of programs that handle the recompression process.

Other Programs

Also in the Guetzli GitHub repository under tools is the guetzli-compare.py heat map program that is created when compiling and can be used to display the differences between the original image and the edited version. The Butteraugli program is also part of Guetzli, in a broader sense, and can be built and installed separately. As far as quantization is concerned, in the Guetzli environment, you will find a number of tools that optimize PNG images and even reduce the size of ZIP and Gzip archives.

In 2013, the developers first presented an algorithm to the public that became known as Zopfli [9]. As with ZIP, Gzip, and PNG, it supports the DEFLATE output format, but does so more thoroughly, which costs more computing power and makes the process particularly interesting for once generated, often read and distributed files. Using this method, the Gimp images are reduced better than with BZIP2; however, the version created by XZ Utils [10] is often even smaller.

The Zopfli suite implements several programs (Table 1). The most universal, advdef (the AdvanceCOMP deflate compression utility), is used to recompress existing images. The current implementation requires a large amount of memory, which limits the size of the input data. The -z switch activates recompression. The -0 to -4 parameters control (increasingly) the degree of recompression.

Table 1

Zopfli Programs

Program

Function

advdef

Recompress PNG, MNG, GZ, TGZ, and SVGZ

advzip

Compress ZIP

advpng

(Re)compress PNG

advmng

Compress MNG (animated PNGs)

With -i <number>, you can influence the number of iterations and, therefore, the optimization indirectly; the default is 15 rounds. Higher values improve the outcome for the -3 and -4 modes but require more computing power. Through -f (force), Advdef creates the output files, even if they are bigger than the input files. The -h option explains which options and parameters control the processing.

In 2015 the developers provided the public with a further development in Zopfli [11]: a variant of LZ77 and Huffman codings that again promises better results. The only program for compression is bro.

Conclusions

Guetzli and its relatives show that the opportunity for innovations can still be found in image editing. In addition to special visual optimizations are the useful methods developed for quantization. In particular, operators of websites that use JPEG or PNG images should check to see whether and to what extent the programs presented here are suitable for their purposes.