Programster's Blog

Tutorials focusing on Linux, programming, and open source

Guetzli - JPEG Image Compression

Guetzli is a new image compression tool that was open sourced by Google in 2017. Here I will show you how to install and use it on Ubuntu Linux which is a lot easier than I expected and takes less than 5 minutes. Other distros will be very similar with only needing to change how you install the necessary packages.

Steps

Install the necessary packages.

sudo apt-get install git gcc make pkg-config libpng-dev

Clone the project

cd $HOME
git clone https://github.com/google/guetzli.git

Build it

cd guetzli
make

Move the final product into your PATH.

sudo cp bin/Release/guetzli  /usr/bin/.

Clean up by removing the source

cd $HOME
rm -rf guetzli

Usage

The easiest way to use the tool is like so which will use a quality rating of 95.

guetzli --quality 95 input.jpg output.jpg

You can think of the quality parameter as a percentage between 1 and 100. However, if you provide a quality level below 84 you will get the following error message:

Guetzli appears to be single threaded, so if you wish to batch process lots of images on a multi-core server, use something to run a number of jobs in parallel equal to the number of threads your CPU supports. Just bear in mind that it can take a noticeable amount of memory.

If you use quality less than 84, you get the following error message:

Guetzli should be called with quality >= 84, otherwise the
output will have noticeable artifacts. If you want to
proceed anyway, please edit the source code.
Guetzli processing failed

You can get help with how to use the tool just by executing the guetzli command with no parameters:

Guetzli JPEG compressor. Usage: 
guetzli [flags] input_filename output_filename

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

Testing and Benchmarking

To test the tool's effectiveness, I decided to test compressing this picture of a book of text from the fabulous Pexels site. This is because JPEG compression is not well suited to handling text which is usually made up of thin sharp lines. For these types of images, PNG may be a better format to use. JPEG images are best suited to photos of the natural world, than something like a desktop screenshot.

Since most of us don't have 4k displays, I decided to use GIMP to shrink the image down to 1620 x 1080 (100% quality) which should fit within a normal 1920 x 1080 display. This shrank the original size from 3.5 MB to a 1.7 MB image which just goes to show that sometimes the best thing you can do is to shrink an image before using compression.

Next I compressed the shrunken image using a quality setting of 84 with guetzli and gimp and compared the results.

This amount of compression is impressive as it means you could store 5.3 times as many images instead of just 3.7 times as many with Gimp.

Of course, all this compression is useless if the output image is noticeably uglier. I'll let you decide for yourself with the image below which has all files opened side-by-side and zoomed in on the right page. I can't tell the difference so I'll be using guetzli from now on.

[click the image to expand]

References