There are many GUI applications on Linux that help optimize and compress images while retaining the original quality. In this article we will use two command-line based utilities , jpegoptim and OptiPNG. Let’s get started.
Jpegoptim is a command line utility that help compress and optimize jpg, jpegs as well as jfif files without losing the original quality. Lossless optimization is supported as well.
Since Jpegoptim is available on the official Ubuntu repository, it can be installed using the apt-get package manager tool.
sudo apt-get update
Now to install jpegoptim , run the command below :
sudo apt-get install jpegoptim
To compress an image using Jpegoptim without losing the original quality, the syntax is as follows :
For instance the command below will optimize our example file (lion.jpg). However before doing so let us first find out the size using the du command:
du -sh /home/net2_admin/lion.jpg
As you can see, the actual size of the jpg image above is 1.7M.
If we now run the command below to compress the image lion.jpg :
This has resulted in optimizing the original image by 15.45%. Since this overwrites the original file, make sure to perform a backup before launching the command above.
Now try to open the compressed image to see if there are quality differences. Normally the original and compressed images should have the same quality.
If you think you can optimize further the compressed image, you can re-run the command above but this will not make any difference as you can see below :
To prevent replacing the original image when you run the command above, you can save the compressed image to another directory as follows.
$ jpegoptim -d ./[destination_directory] -p [image_file_name].jpg
jpegoptim -d ./net2_images_folder -p new_file_name.jpg
This will compress new_file_name.jpg file and save it to the older net2_images_folder.
If you do not care about the quality after compression, you can specify how much loss in quality suits you. There are actually two options to “lossily” compress your images :
Using this utility, you would also be able to compress the original image to a desired size. This however will disable the lossless optimization and will therefore result in quality loss as expected.
For instance, let’s compress the image above from 1.7MB to around 500KB.This can be done by using the command :
jpegoptim –size=500k /home/net2_admin/lion.jpeg
The size has indeed been reduced.
b. Using the -m flag:
This option will disable lossless optimization mode and will thus lower the original quality. To specify the compression percentage, run the command as follows :
jpegoptim -m[percentage] your_original_image.jpg
Before compressing an image, you have the possibility to simulate image compression so that you get an idea about what to expect. The following command will provide an estimate about the expected size after compression :
jpegoptim -n /home/net2_admin/lion.jpeg
The expected reduction will be around 15.45% as shown in the simulation command above. Notice the original file size has not been changed.
A – Using a single command
To compress many images in a directory, change to that directory and run the command below in order to compress all the images at once:
To compress multiple images , run the command as follows:
jpegoptim img1.JPG img2.JPG img3.JPG
B – Using a script
To compress many images at once, it is also possible to use a script. This shell script will be able to compress all jpg images in the current directory and store them in a folder with the name “compressed_images”.
To create the script, run the command below which will open an empty script file named compressmyjpg.sh :
Once it is in edit mode, add the following codeline :
for i in *.jpg; do jpegoptim -d ./compressed_images -p “$i”; done
After you are done with the script, exit and save. To it executable by the current user, execute the command below:
chmod +x compressmyjpg.sh
To run the script, you would need to change to the directory where your images are available. As mentioned above, make sure to have the destination folder named compressed_images created already.
Now you should be able to execute the script as follows:
Once this is completed, you should be able to see all the compressed images in the directory “compressed_images ”.
For additional information about jpegoptim, visit the man pages by running :
OptiPNG is a command line utility used to compress PNG images without losing the original quality.
To install OptiPNG, run the command below :
sudo apt-get install optipng
The basic syntax of OptiPNG is:
optipng [options] image_file_name.png
Before compressing an example PNG image, let’s find out its actual size :
ls -lh /home/net2_admin/lion.png
As you can see, the file size of the image calc.png is 3.2M. Let’s now compress it using OptiPNG:
From the output aboe, the size of the image lion.png has been reduced by 12.99%. To check its new size, run the command below :
ls -lh /home/net2_admin/lion.png
If you try to open the new file that has just been compressed, you will not notice any visible quality differences since the new file should be of the same quality as the original.
In order to compress multiple PNG images with one command using optiPNG, change to the directory which contains all images and run the command:
For more details about optipng check man pages.
To compress or optimize the size of images on Linux, you can use either jpegoptim for jpg images or OptiPNG for png images. You will be able to save quite a lot of space on the disk yet will not lose the quality of the original images.