Optimize/Compress JPEG files in Ubuntu

In this article, we would discuss how to optimize/compress jpeg files in Ubuntu using command-line utility jpegoptim. There could be various reasons why you may want to optimize your jpeg files. For instance, you could be uploading photos to a cloud storage, sharing it on social media networks etc. The utility would also be useful for those who may want to upload jpeg files on their websites. This will reduce the time required to upload jpeg files and at the same time will lead to substantial reduction in bandwidth needed.

Install jpegoptim in Ubuntu

jpegoptim package is available in standard Ubuntu repository. But, installation will require you to have access to superuser privileges. In case, you don’t have one then contact your System Administrator for assistance. Thereafter, run the following in terminal –

sudo apt update
sudo apt install jpegoptim

Optimize/Compress JPEG files

Before performing any operations using the utility jpegoptim, you should know that all operations will be performed on original file(i.e. input file) by default. If you want to preserve your original file then, you need to use the option --dest which is path to the directory where the optimized file will stored. This will leave the original file as it is. We will explain --dest option in subsequent examples.

The syntax for the utility is –

jpegoptim [options] [file_name]

To optimize your image file quality-wise and save it to a directory leaving your original file intact, use the following command with options --dest and --max

jpegoptim --dest=directory_path --max=<quality> file_name.jpeg

for instance,

jpegoptim --dest=/home/ --max=75 file_name.jpeg

You can provide maximum quality values for an image between 0 and 100. --max option disables the lossless optimization mode.

Also, to optimize an image file size-wise we have to use the --size option –

jpegoptim --dest=directory_path --size=<size> file_name.jpeg

where, <size> could be provided in Kilobytes or as a percentage of the image size.

for instance,

jpegoptim --dest=/home/ --size=500 file_name.jpeg
jpegoptim --dest=/home/ --size=50% file_name.jpeg

In above example, the --size=500 reduces the file to a size of 500 KB. And, --size=50% reduces the size of our image file to half.

In case you are using --dest option and the file already exists then you would have to use -o or --overwrite option to overwrite the existing file.

Note: If you are unable to reduce the file size to the required size then, you need to run the command again on the same file.

Apart from optimizing a jpeg file, we can do perform other operations too like striping all markers, preserving file permissions, preserving file modification times, etc. All these and other options will be discussed in brief.

--preserve preserves file modified time,

-P or --preserve-perms preserves file permissions,

--strip-all is used to strip all markers except Comment and Exif / IPTC / PhotoShop / ICC / XMP markers,

--strip-none to preserve all markers except JFIF (APP0) & Adobe (APP14)

--strip-exif, --strip-iptc,--strip-icc, --strip-xmp to strip respective markers as mentioned in the option.

In conclusion, we have discussed how to optimize/compress jpeg files in Ubuntu using package jpegoptim.

Similar Posts