Methods of payment Abuse

Optimizing JPEGs in Linux

28.11.2022, 20:39

You may have a lot of pictures on your computer and they take up an inordinate amount of space, is there anything you can do about it? For example, compress them without loss of quality. Compression can also be very appropriate in the case of uploading a set of pictures to a cloud server. This is what we will talk about in our article.

Method of image compression

Many different graphic tools are known that allow you to solve this problem, but in fact everything is much simpler. How to compress images with the help of utilities?

There are two full-fledged options:

  • jpegoptim;
  • optipng.

JPEG - the most common picture format, in this article we will consider the compression of this image format. Therefore, let's consider working in jpegoptim, thanks to which it will be possible to perform compression of pictures of this format.

How to perform compression of pictures

The program jpegoptim - a complete tool that allows you to use for compression of JPEG, JPG and JFIF format. And compression occurs without loss of quality. The utility optimizes pictures based on the principles of the Huffman table. In the algorithm JPEG compression occurs, but it works in a way that reduces the quality. Compression with jpegoptim does not harm the quality of the picture.

Installing the program

To install the program in Ubuntu you need to execute:

$ sudo apt install jpegoptim

In Fedora, CentOS and similar distributions, you must first add the epel-release repository.

This can be done with the command:

$ sudo yum install epel-release

Or:

$ sudo dnf install epel-release

You can install jpegoptim from a recently added repository:

$ sudo yum install jpegoptim

Or:

$ sudo dnf install jpegoptim

How to use the utility

Applying the program on Linux is not too difficult. You need to execute the program and pass the file name.

The syntax would be as follows:

$ jpegoptim опции имя_файла.jpeg

Consider the options of the utility:

сжатие изображений

How to optimize a picture using the program?

First, let's look at the original size:

$ du image.jpg

сжатие jpeg

Perform the simplest optimization:

$ jpegoptim image.jpg

сжатие jpeg

Let's see the size:

$ du image.jpg

Optimizing JPEGs in Linux

The jpg compression has occurred, open the picture with any program and see that the quality has not changed at all. The source and the finished image are identical.

On the left is the image after compression, on the right is the original:

Optimizing JPEGs in Linux

Need to compress an image to a certain size?

You have to sacrifice quality:

$ jpegoptim --size=250k image.jpeg

The picture will be compressed, but the quality will suffer. If you have a lot of pictures, use batch compression: go to the folder with pictures, run the compress command

$ cd images/

$ jpegoptim *.jpg

Optimizing JPEGs in Linux

You can also list several images at once:

$ jpegoptim image1.jpg image2.jpg

To optimize all pictures in a particular directory, you can command xargs along with find. For example, optimize all images in a directory and then move them ~/compressed:

$ find . -name "*.jpg" | xargs jpegoptim -d ~/compressed -p

Compressing jpg images in Linux is very easy and you could see that. In the next article we will tell you how to compress png.