Optimize JPEG/JPG Images in Ubuntu with Jpegoptim

The image resolution capability of today’s photo capturing devices like smartphones and digital cameras is increasing day by day. The real deal comes when we have to share these images, upload them on a cloud facility, or even save them on a device with restricted storage. Graphic designers also have to deal with the same issue but they are well-equipped with tools that can compress the images so that they can be easily shared with the stakeholders.

For Ubuntu, there are many graphical utilities that would let you optimize and compress your jpegs but here we will be discussing a command-line tool called Jpegoptim. This is especially useful for the Terminal-savvy who like to perform their tasks using minimum system resources. Jpegoptim can help you in compressing your jpegs, jpg, and jfif files with and without quality loss, depending on what you are looking for.

In this article, we will describe how you can install the command line utility Jpegoptim on Ubuntu. We will also explain various ways in which you can use this application to optimize and compress your jpeg image files. The article will also enable you in writing and executing a bash script that will automate the process of compressing all jpegs located in a single directory.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.

Installing Jpegoptim on Ubuntu

Jpegoptim is easily available through the official Ubuntu repository and can be easily installed through the command line using the apt-get command.

Open your Terminal application either through the system Application Launcher Search or through the Ctrl+Alt+T shortcut.

The next step is to update your system’s repository index through the following command:

$ sudo apt-get update

Update Ubuntu Package Lists

This helps you in installing the latest available version of a software from the Internet. Please note that only an authorized user can add, remove and configure software on Ubuntu.

Now you are ready to install Image Jpegoptim; you can do so by running the following command as sudo:

$ sudo apt-get install jpegoptim

Install jpegoptim

The system might ask you the password for sudo and also provide you with a Y/n option to continue the installation. Enter Y and then hit enter; the software will be installed on your system. The process may, however, take some time depending on your Internet speed.

You can check the version number of the application, and also verify that it is indeed installed on your system, through the following command:

$ jpegoptim --version

Check installed jpegoptim version

Using Jpegoptim for Image Compression

Let us now explore the power of Jpegoptim through the following ways you can use it:

Losslessly compress a single image

If you want to compress a single image without wanting to compromise on the quality, use the following command:

$ jpegoptim image_name.jpg

Example:

$ jpegoptim sample.jpg

Optimize a JPEG image file

Note: Please keep in mind that the tool overwrites the existing image, so it is a good idea to have the original image saved somewhere else.

If you want the image to be compressed even further, you can run the same command again. However, Jpegoptim will start skipping the compression if it reaches the limit where you have a lossless image quality with the most compression.

Here is how the output would look like in such a situation:

File is already optmized

Compress an image to another folder

If you are afraid that you will lose your original image because Jpegoptim will overwrite it, you can tell the tool to save the compressed image to another folder.

First, create a folder where you want the compressed images to be saved. If you want them to be saved to an already existing folder then you do not need to create a new one. You can then use the following command syntax to perform the compression:

$ jpegoptim -d ./[destination-folder] -p [image_name].jpg

For example:

$ jpegoptim -d ./compressed -p sample.jpg

Save compressed file to different folder

The above command will compress and save my sample.jpg file to an already existing folder named “compressed”.

Check Potential Compression

If you want to see, beforehand, how much an image is capable of being compressed, you can simulate image compression through Jpegoptim. The following command will not actually compress the image, it will just tell you how much the image will be compressed if you ever try to compress it with Jpegoptim.

The following example will further explain the process:

I have a jpg image named sample.jpg. I will print its size first through the du command as follows:

$ du sample.jpg

Then, I will use the -n flag with the jpegoptim command to know about its potential compression.

$ jpegoptim -n sample.jpg

Check how much a JPEG file can be compressed

When I rechecked its size after using the jpegoptim command as above, I saw no change in its size. This ensures that the jpeg command with the -n flag only displays the future compression percentage and size without actually compressing the image.

Compress Images “with” a loss in quality

Sometimes we want to compress our images knowing that the process will not be lossless. The good thing is that with Jpegoptim you can specify how much quality loss you are ok with. There are two ways you can “lossily” compress your images;

1. Using the -m flag to specify quality factor/percentage: Sets the maximum image quality factor (disables lossless optimization mode, which is by default enabled). This option will reduce the quality of those source files that were saved using a higher quality setting. While files that already have a lower quality setting will be compressed using the lossless optimization method.

Here is how you can specify the compression percentage:

$ jpegoptim -m[percentage_in_numbers] image.jpg

Lossy compression of JPG files

This was my original image:

Original image sample

And this is how it looks with 10 percent quality factor:

10% quality factor of jpg compression sample

2. Using the --size option to specify the size of the compressed image: Try to optimize file to a given size (disables lossless optimization mode). Target size is specified either in kilobytes (1 - n)

or as a percentage (1% - 99%) of the original file size.

This is how you can specify size in kbs for the resulting image:

$ jpegoptim --size=[size-in-kb] image_name.jpg

Define size of optimized image

Batch Optimizing Image files

There are several ways in which you can use Jpegoptim to compress multiple files at once.

The first one is to specify all the files you want to compress in a single jpegoptim command as follows:

$ jpegoptim file1.jpeg file2.jpg file3.jpg

The command will overwrite all the specified files and with the newly compressed ones.

The second method is to tell Jpegoptim to compress all the files of a certain type, for example jpg, all residing in the same folder:

$ jpegoptim *.jpg

In order to see how this command works, I listed all the files in my Pictures folder with the -l flag. This command would print the total size of all the images residing in the current folder:

$ ls -l

List of photos that shall be optimized

Then I used the same ls -l command to see how much difference has the Jpegoptim command made to the total size of all files. You can see the remarkable difference in total bytes in the following output:

Optimize all jpg files in a folder

These were just two files; you can save a lot of space like this. The beauty of the whole process is that you are not compromising on the quality of your valuable images. You can, of course, do the same with other file formats supported by Jpegoptim.

The third and a very useful option is that you can even make use of a shell script to automate compressing all files of a single type located in the current directory. Here is how to do so:

Bash Script to compress all jpg files in the current directory

This section will explain how you can write a shell script that will compress all jpgs located in the current directory to a folder named “compressed”. The script will not create the folder; it will just save the output to an already created folder by this name.

First, move to the Documents folder where we will create the script:

$ cd Documents

Note: You can save the script anywhere you like. It is advisable that you create all the scripts in the same folder so that you do not lose track of where you have saved them. I personally prefer saving all my scripts in the Documents folder.

Create the Script

Open a new script file in one of your favorite text editors. We will use the nano editor in order to open an empty script file by the name of compressAll.sh

$ nano compressAll.sh

In that empty file, add the following script.

#!/bin/sh
# compress all *.jpg files in the current directory
# and place them in ./compressed directory
# with the same modification date as original files.

for i in *.jpg; do jpegoptim -d ./compressed -p "$i"; done

Tip: Instead of typing the whole script into you bash file, you can copy it from here and paste in the Terminal by using the Ctrl+Shift+V, or by using the Paste option from the right-click menu.

This is how your file would look like:

Jpeg batch optimization script

Now, exit the file through the Ctrl+X shortcut. You will then be asked to save the file on the “Save modified buffer?” prompt. Type Y and then hit Enter; your script file is now saved in the current folder.

In order to make this file executable by the current user, run the following command in your Terminal:

$ chmod +x compressAll.sh

In order to run the script, change your current directory to the one whose jpgs you want to compress(in my case the Pictures directory). Make sure that the directory contains a folder named “compressed”. If it is not there, please create one before running the script.

Run the script

Finally, run the script as follows:

$ /home/[username]/Documents/compressAll.sh

This command will execute the bash script from the folder you created it in. This is the reason you had to specify the entire path to that .sh file.

Run script

You will see that all the compressed files will be written in the “compressed” folder. You can verify this through the following command:

$ ls compressed

This was all about image optimization through the Jpegoptim utility. For further details you can see the manpage through the following command:

$ man jpegoptim

Now sharing your images over the Internet and uploading them to bandwidth and storage restricted location should not be a problem