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.
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:
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.
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.
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
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
Perform the simplest optimization:
$ jpegoptim image.jpg
Let's see the size:
$ du image.jpg
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:
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
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.