The jpegoptim is a command line tool that allows to compress JPEG and JPG images. This tool provides lossless optimization based on Huffman tables and lossy optimization where user can specify the maximum quality.
This tutorial explains how to install jpegoptim on Raspberry Pi.
Use SSH to connect to Raspberry Pi. Run the following commands to update the package lists and install jpegoptim:
sudo apt update
sudo apt install -y jpegoptim
After the installation is finished, we can check jpegoptim version:
jpegoptim --version
Now download image from the Internet and create a new directory where compressed images will be stored:
wget -O test.jpg https://raw.githubusercontent.com/mozilla/mozjpeg/master/testimages/testorig.jpg
mkdir compressed
Run the jpegoptim
command to compress the image. By default, command overwrites the original image. We can use --dest
option to specify 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-r--r-- 1 pi pi 5463 Mar 5 05:28 compressed/test.jpg
-rw-r--r-- 1 pi pi 5770 Mar 5 05:28 test.jpg
We can see that an image is ~5% smaller than the original image.
We can also use lossy optimization mode for image compression. The maximum image quality can be specified between 0 (worst) and 100 (best) by using --max
option. This disables lossless optimization mode.
The --overwrite
option allows to overwrite output file if it already exists.
jpegoptim --max=70 --overwrite test.jpg --dest=compressed
We can use --size
option. Then jpegoptim tries to compress the image to given size (disables lossless optimization mode).
Output size can be specified in kilobytes (1 - n):
jpegoptim --size=2 --overwrite test.jpg --dest=compressed
Or we can specify the output size as percentage (1% - 99%):
jpegoptim --size=50% --overwrite test.jpg --dest=compressed
Use the following command if you want to completely remove jpegoptim:
sudo apt purge --autoremove -y jpegoptim
Leave a Comment
Cancel reply