Optimize jpeg / jpg images using the Debian command line

s2 0

The image resolution of today’s photo devices such as smartphones and digital cameras is increasing day by day. The real trick is to share these images, upload them to a cloud, or even store them on a device with limited storage space. Graphic designers struggle with the same problem, but come with tools that can compress the images so they can be easily shared.

There are many graphical tools for Debian that you can use to optimize and compress your JPEGs, but here we are going to discuss a command line tool called Jpegoptim. This is particularly useful for terminal-savvy users who want to perform their tasks with minimal system resources. Jpegoptim can help you compress your JPEG, JPG and JFIF files with and without quality loss depending on what you are looking for.

In this article we describe how to install the command line utility Jpegoptim on your Debian. We’ll also explain various ways you can use this program to optimize and compress your JPEG image files. The article also enables you to write and run a bash script that automates the process of compressing all of the JPEG files that are in a single directory.

We ran the commands and procedures mentioned in this article on a Debian 10 Buster system.

Install Jpegoptim on Debian

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

Open your terminal application via the System Application Launcher search as follows:

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

$ sudo apt-get update

This will help you install the latest version of software available from the internet. Please note that only an authorized user can add, remove and configure software on Debian.

Now you can install Image Jpegoptim. You can do this by running the following command as sudo:

$ sudo apt-get install jpegoptim

Install JPEGoptim

The system may ask you for the sudo password and also give you a Y / N option to continue the installation. Type Y then hit Enter; The software will be installed on your system. However, the process may take some time depending on your internet speed.display

You can check the version number of the application and also check that it is actually installed on your system by running the following command:

$ jpegoptim --version

Check jpegoptim version

Using JPEG Optimized for Image Compression

Now let’s explore the power of Jpegoptim through the following uses:

Compress a single image without loss

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

$ jpegoptim image_name.jpg


$ jpegoptim sample.jpg

Optimize JPEG image

Note: Please note that the tool will overwrite the existing image, so it is advisable to save the original image somewhere else.

If you want the image to compress even further, you can run the same command again. However, Jpegoptim starts to skip compression when it reaches the limit where you have lossless image quality with the highest compression.

This is what the output would look like in such a situation:

Reduced size of the JPEG image

Compress an image into a different folder

If you are concerned that you will lose your original image because JPEGoptim will overwrite it, you can instruct the tool to save the compressed image in a different folder.

First, create a folder in which to save the compressed images. If you want them to be saved in an existing folder, you don’t 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 the optimized image in a different folder

The above command compresses and saves my sample.jpg file in an existing folder called “compressed”.

Check potential compression

If you want to see in advance how much an image can be compressed, you can simulate the image compression with JPEGoptim. The command below doesn’t actually compress the image, it just tells you how much the image will be compressed if you ever try to compress it with jpegoptim.

The following example is intended to further explain the process:

I have a JPG image called sample.jpg. I’ll print its size first via the du command like this:

$ du sample .jpg

Then I use the -n flag with the jpegoptim command to learn more about the possible compression.

$ jpegoptim -n sample.jpg

Review the space savings that JPEG images can save

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

Compress images “with” loss of quality

Sometimes we want to compress our images knowing the process is not lossless. The good thing is that at Jpegoptim you can indicate how much loss of quality you are okay with. There are two ways you can “losslessly” compress your images;

1. Using the -m flag to specify the quality factor / percentage: Specifies the maximum image quality factor (deactivates the lossless optimization mode, which is activated by default). This option decreases the quality of the source files saved with a higher quality setting. Files that already have a lower quality setting are compressed using the lossless optimization method.

Here’s how you can specify the compression percentage:

$ jpegoptim -m[percentage_in_numbers] picture.jpg

Lossy Compression

This was my original picture:

Test image

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

Compressed image

2. Use the –size option to specify the size of the compressed image: Try to optimize the file to a certain size (disable lossless optimization mode). The target size is specified either in kilobytes (1 – n)

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

Here’s how you can specify the size in kbs for the resulting image:

$ jpegoptim –size =[size-in-kb] Picturename.jpg

Batch optimization of image files

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

The first is to specify all of the files you want to compress in a single jpegoptim command like this:

$ jpegoptim file1.jpeg file2.jpg file3.jpg

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

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

$ jpegoptim *.jpg

To see how this command works, I’ve listed all of the files in my pictures folder with the -l flag. This command would print the total size of all images that are in the current folder:

$ ls -l

List of pictures

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

Optimize all files in a specific folder

These were just two files; You can save so much space. The beauty of the whole process is that you don’t compromise on the quality of your precious images. You can of course do the same with other file formats supported by Jpedoptim.

The third and very useful option is that you can even use a shell script to automate the compression of all files of a single type that are in the current directory. That’s how it’s done:

Bash script to compress all JPG files in the current directory

This section explains how you can write a shell script that compresses all of the JPGs in the current directory into a folder called “compressed”. The script does not create the folder; it will only save the output in an already created folder with that name.

First, switch to the Documents folder, in which we will create the script:

$ cd Documents

Note: You can save the script anywhere you want. It’s a good idea to create all of your scripts in the same folder so that you don’t lose track of where you’ve saved them. Personally, I prefer to keep all of my scripts in the Documents folder.

Open a new script file in one of your favorite text editors. We’re going to use the nano editor to create a blank script file called CompressAll.sh. to open

$ nano compressAll.sh

In this empty file, add the following script.

#! / bin / sh

# Compress all * .jpg files in the current directory

# and put it in the ./compressed directory

# with the same modification date as the original files.

for i in * .jpg; do jpegooptim -d ./compressed -p “$ i”; done

Top: Instead of putting the entire script in your bash file, you can copy and paste it from here in Terminal by using Ctrl + Shift + V or using the Paste option from the context menu.

Your file would look like this:

Script to compress images with jpegoptim

Now exit the file using the key combination Ctrl + X. You will then be asked to save the file under “Save changed buffer?”. prompt. Type Y then hit Enter; Your script file will now be saved in the current folder.

To make this file executable by the current user, run the following command in your terminal:

$ chmod +x compressAll.sh

To run the script, change your current directory to the one whose JPGs you want to compress (in my case the images directory). Make sure the directory contains a folder called “compressed”. If it doesn’t exist, create one before running the script.

Finally, run the script as follows:

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

This command runs the bash script from the folder where you created it. Because of this, you had to provide the full path to this .sh file.

Compression script in action

You will see that all compressed files are written to the “compressed” folder. You can check this with the following command:

$ ls compressed

Here everything revolved around image optimization using the Jpegoptim utility. For more details, you can access the man page with the following command:

$ man jpegoptim

Sharing your pictures over the internet and uploading them to a location with limited bandwidth and space shouldn’t be a problem.

Optimize jpeg / jpg images using the Debian command line