Install jpegoptim on Ubuntu 20.04

The jpegoptim is a tool that enables to compress JPEG and JPG images via command line. It supports lossless optimization based on Huffman tables and lossy optimization where user can specify the maximum quality.

This tutorial demonstrates how to install jpegoptim on Ubuntu 20.04.

Install jpegoptim

Update the package lists:

sudo apt update

Run the following command to install jpegoptim:

sudo apt install -y jpegoptim

Once installation is completed, we can check version of jpegoptim:

jpegoptim --version

Testing jpegoptim

For testing purpose, download the image:

wget -O test.jpg https://raw.githubusercontent.com/mozilla/mozjpeg/master/testimages/testorig.jpg

Create a new directory to store compressed images:

mkdir compressed

Execute the jpegoptim command to compress the image. By default, original image is overwritten. The --dest option can be used to provide the destination directory for compressed images. By default, lossless optimization mode is used.

jpegoptim test.jpg --dest=compressed

We can check the image sizes:

ls -l test.jpg compressed/test.jpg
-rw-rw-r-- 1 adminer adminer 5463 Jan 15 03:28 compressed/test.jpg
-rw-rw-r-- 1 adminer adminer 5770 Jan 15 03:15 test.jpg

As we can see, image is ~5% smaller than the original image.

Lossy optimization mode for image compression can be used too. Using --max option we can specify the maximum image quality between 0 (worst) and 100 (best). It disables lossless optimization mode.

The --overwrite option allows to overwrite output file if it already exists.

jpegoptim --max=70 --overwrite test.jpg --dest=compressed

The jpegoptim command supports --size option. When it specified, command tries to compress the image to given size (disables lossless optimization mode).

Output size can be given in kilobytes (1 – n):

jpegoptim --size=2 --overwrite test.jpg --dest=compressed

Or output size can be specified as percentage (1% – 99%):

jpegoptim --size=50% --overwrite test.jpg --dest=compressed

Uninstall jpegoptim

Run the following command if you decided to completely remove jpegoptim and related dependencies:

sudo apt purge --autoremove -y jpegoptim

Leave a Comment

Your email address will not be published.