Compressing Images On The Linux Command Line

Published on: Tue, Jan 26, 2016 at 4:51 am EST
Linux Guides

If you have a lot of high-resolution images stored on your Linux computer, you may be running out of storage due to the large amount of disk space used by your photo library. In this case, you may be looking for a solution that allows you to compress these images to conserve disk space without affecting their quality.

Further, traditional compression solutions often allow compressing image files one-by-one, which can be a rather cumbersome and time-consuming process. Therefore, compressing your images in batches may be a great idea to save time and effort.

That's where jpegoptim and OptiPNG come in handy. Both jpegoptim and OptiPNG are command line-based utilities that can be used to optimize and compress pictures in batches without affecting their original quality (also known as lossless compression). Jpegoptim handles JPEG files while OptiPNG processes pictures in the format of PNG, BMP, GIF, PNM, and TIFF.

Let's go over the process of installing jpegoptim and OptiPNG to compress pictures in Linux using the command line.

Note: This tutorial assumes you are using a non-root user with sudo privileges.

Installing jpegoptim and OptiPNG

On CentOS or other RPM-based Linux distributions:

sudo yum install epel-release
sudo yum install jpegoptim optipng

On Debian or other APT-based Linux distributions:

sudo apt-get install jpegoptim optipng

Using jpegoptim to compress JPEG files

In this example, your original JPEG (.jpg) files are stored ~/jpeg.

a) If you want to compress a single file, let's say example.jpg you would be using the below command:

cd ~/jpeg
jpegoptim example.jpg

Notice: The original example.jpg will be replaced with a compressed version by default.

b) If you want to compress all .jpg files in the source directory:

cd ~/jpeg
jpegoptim *.jpg

All the original files will be overwritten and replaced with the compressed version.

c) If you wish to keep the original files, you can specify a target directory to store the compressed version to as follows:

cd ~/jpeg
mkdir optim
jpegoptim *.jpg -d ~/jpeg/optim

d) For more information on how to use the jpegoptim command, you can use the -h flag to view its help file:

jpegoptim -h

Using OptiPNG to optimize PNG, BMP, GIF, PNM, and TIFF files

Just like jpegoptim, OptiPNG will overwrite the original files and replace then with the compressed version in the source directory by default. Meanwhile, for BMP, GIF, PNM, and TIFF files, OptiPNG will generate optimized .png version of the file with the same name, leaving the original files in tact. These file would have to be manually removed one the compression process is compelted.

Below are a few examples on how to use OptiPNG to apply lossless compression to your images:

In this example, your original JPEG (.jpg) files are stored ~/pic.

a) If you want to compress a single file, let's say example.png you would be using the below command:

cd ~/pic
optipng example.png

Notice: The original example.png file will be replaced with a optimized version.

b) Optimize a .bmp file example.bmp:

cd ~/pic
optipng example.bmp

A file named example.png will be created in the source directory while the original example.bmp stays in tact.

c) If you want to compress all .png files in the source directory:

cd ~/pic
optipng *.png

All the original files will be overwritten and replaced with the compressed version.

d) If you prefer to keep the original files in tact, you can do so by using the -keep flag as follows:

cd ~/pic
optipng -keep *.png

The original files will remain in tact and suffixed with a .bak. For example example.png.bak.

e) If you wish to keep the original files, you can specify a target directory to store the compressed version to as follows:

cd ~/pic
optipng -dir ~/pic/optim *.png

All the original files will remain in tact in the source directory while the compressed version will be saved in target directory.

f) For more information on how to use the OptiPNG command, you can use the -h flag to view its help file:

optipng -h

This concludes our tutorial.

Want to contribute ?

You could earn up to $300 by adding new articles