Compress image files with Guetzli
Small Wonders
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.
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.
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.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.