Compress image files with Guetzli

Small Wonders

© Lead Image © Stuart Monk,

© Lead Image © Stuart Monk,

Article from Issue 207/2018

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

$ guetzli
Guetzli JPEG compressor. Usage:
guetzli [flags] input_filename output_filename
  --verbose    -- Print a verbose trace of all attempts to standard output.
  --quality Q  -- Visual quality to aim for, expressed as a JPEG quality value.
  --memlimit M -- Memory limit in MB. Guetzli will fail if unable to stay under
                  the limit. Default is 6000 MB
  --nomemlimit -- Do not limit memory usage.

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.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95