compress images in linux

How to Compress Images in Linux

If you have a lot of images that you want to transfer or store, you may need to compress them to save disk space. There are plenty of tools available for this purpose in Linux. In such cases, command line tools are more useful than GUI tools because you can easily do bulk compression via terminal, or using shell script. You can even embed the compression code into your application code to automate it. In this article, we will learn how to compress images in Linux using jpegoptim and OptimPNG commands.


How to Compress Images in Linux

Here is how to compress images in Linux using jpegoptim and OptimPNG utilities. They both compress images without losing its quality.


1. Using Jpegoptim

Jpegoptim is a command line tool that allows you to easily optimize & compress JPEG, JPG and JFIF images, using lossless compression. It uses Huffman algorithm for image compression.

Open terminal and run the following command to install jpegoptim tool.

Debian/Ubuntu

# apt-get install jpegoptim
or
$ sudo apt-get install jpegoptim

Redhat/CentOS/Fedora/SUSE

In case of RHEL/CentOS/Fedora/SUSE Linux, you need to enable epel-release before you install jpegoptim tool.

# yum install epel-release
# dnf install epel-release    [On Fedora 22+ versions]

Next, install jpegoptim command with the following command.

# yum install jpegoptim
# dnf install jpegoptim    [On Fedora 22+ versions]

Once you have installed jpegoptim, you can edit images using the following command.

$ jpegoptim [options] /path/to/image/file

For example, if you want to compress the image /home/ubuntu/test.jpg, run the following command.

$ jpegoptim /home/ubuntu/test.jpg

Please use full path to your image file, else it will look for the image relative to your present working directory. If you don’t use any option, it will automatically compress and optimize your image.

Also, please note, jpegoptim will overwrite your existing file by default.

You can also specify the size of your compressed image if you want but it will not be lossless compression, in that case.

Here is an example to compress your image to 500kb.

$ jpegoptim --size=500k /home/ubuntu/test.jpg

Batch Compress Multiple Images

You can also use jpegoptim to compress multiple images at once. Just list them one after the other in a space separated manner. Here is an example to compress 3 image files in a single command.

$ jpegoptim /home/ubuntu/test1.jpg /home/ubuntu/test2.jpg /home/ubuntu/test3.jpg

You can also use wildcard characters to mention multiple files. For example, here is a command to compress all .jpg images in /home/ubuntu.

$ jpegoptim /home/ubuntu/*.jpg

For more details about jpegoptim command, use its man pages.

$ man jpegoptim


2. Using OptiPNG

jpegoptim doesn’t work with PNG files. OptiPNG works with PNG files only. Open terminal and run the following command to install OptiPNG in your system.

Debian/Ubuntu

# apt-get install optipng
or
$ sudo apt-get install optipng

Redhat/CentOS/SUSE/Fedora

In this case also, you need to enable epel repository first.

# yum install epel-release
# dnf install epel-release    [On Fedora 22+ versions]

Next, install OptiPNG.

# yum install optipng
# dnf install optipng    [On Fedora 22+ versions]

Once you have installed OptiPNG, you can easily optimize and compress images using the following syntax.

$ optipng [options] /path/to/image/file

For example, here is the command to compress image /home/ubuntu/test.png

$ optipng /home/ubuntu/test.png

Batch Compress Multiple Images

Like jpegoptim, you can compress multiple images by mentioning them one after the other in a space separated manner. Here is an example to compress 3 images using single command.

$ optipng test1.png test2.png test3.png

You can also use wildcard characters to compress multiple images present in a folder. Here is a command to compress all png images in /home/ubuntu.

$ optipng /home/ubuntu/*.png

You can get more details about optipng using its man pages.

$ man optipng

In this article, we have learnt how to compress images in Linux using couple of command line tools jpegoptim and optipng. Since they are command line tools, you can easily use them for batch compression of multiple images, as well as automate image compression by adding them to a shell script.

Also read:

How to Reduce PDF Size in Linux
How to Password Protect PDF in Linux
How to Remove PDF Password in Linux
How to Uninstall NVIDIA Drivers in Linux
How to Build Deb Package in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *