How to Compress JPEG Images using Terminal on Linux

How to Compress JPEG Images using Terminal on Linux

Looking for tools to compress JPEG images using Terminal on Linux? This guide introduces you to a few and how to use them.

Image compression is the process of reducing the file size of an image without significantly compromising its quality. Mobile cameras nowadays capture photos that can be up to 5 or 6 MBs. Compressing images is a quick way to save disk space and bandwidth. It’s also helpful before converting to PDFs to keep the size down.

While there are many image editing tools, they aren’t easy to use. This tutorial will show you how you can compress JPEG images from your Linux terminal using CLI tools.

Let’s get started!

Compress JPEG Images using Terminal on Linux with Jpegoptim

Jpegoptim is a simple CLI utility used for image compression. It takes many arguments, giving you more control over the compression process. It can work on multiple images or a batch of images in a directory.

First, let’s Jpegoptim. To do that, simply run this command:

$ sudo apt-get install jpegoptim

Output:

install Jpegoptim

It’s a small file so shouldn’t take too long to install. Now let’s get a photo to test out the tool. We chose this butterfly photo for the test. It’s over 11 MB in size.

image size linux

The easiest way to compress an image with this tool is to use the jpegoptim command with the image name as an argument. First, go to the directory where the image is located using this command:

$ cd [Image destination]

Now compress the image with this command:

$ jpegoptim [image name]

In our case, the command looks like this:

$ jpegoptim butterfly.jpg

Output:

Compress JPEG Images using Terminal on Linux

The image was compressed by 17.25% and became 9.2 MB. This method compresses the image losslessly.

compressed image size linux

But in many cases, we don’t want that. We want it to become kilobyte sized. We can do that as well. In the above command, we can set the size argument to get the exact size we want. Let’s say, we want the size to be 100 kilobytes. To do that, we use the command in this way:

$ jpegoptim --size=100k butterfly.jpeg

Output:

Compress JPEG Images using Terminal on Linux

Let’s check the size of the image again.

compressed image size linux

As expected, it’s nearly the size we wanted it to be. Let’s also see the image quality and compare it to the original.

compressed image

The difference is also obvious. If you prefer using percentages instead of setting a fixed size, that’s also possible. In that case, the command will look like this:

$ jpegoptim --size=50% butterfly.jpeg

Output:

Compress JPEG Images using Terminal on Linux

After 50% compression, the image should be around 5-6 MB. Let’s see if it matches the expected size.

compressed image size linux

You can also compress several images at once. All you have to do is add all images you want to edit as arguments. So in this example, this could be a valid command to compress multiple images:

$ jpegoptim butterfly1.jpg butterfly2.jpg butterfly3.jpg

Or if you need to process a whole lot of images, you can move all of them into a single directory. To do so, use this command:

$ mv /path/to/source/*.jpg /path/to/destination/

Now go into that directory using this command:

$ cd /path/to/destination/

Finally, run this command:

$ jpegoptim *.jpeg

This command compresses all available JPEG images in the current directory. Another useful feature of this tool is setting the compressed images’ destination. This allows you to keep both the original and edited version and move the edited versions to another directory. To achieve this, run the below command:

$ jpegoptim --dest=compressed *.jpg

This command will compress all JPEG files in the current directory and automatically move them to a directory called ‘compressed’.

If you prefer disabling the lossless optimization mode and performing a lossy compression, then you can use the -m flag, like this:

$ jpegoptim -m50 butterfly.jpg 

Output:

Compress JPEG Images using Terminal on Linux

This sets the maximum image quality factor at 50%.

One cool trick for this tool is to simulate compression before actually compressing an image. For that, run this command:

$ jpegoptim -n [photo name]
$ jpegoptim -n butterfly.jpg

Output:

Compress JPEG Images using Terminal on Linux

If you check the image resolution, it should stay the same. This is useful to test results without doing anything to the file.

You can learn more about Jpegoptim on its manual page.

Compress JPEG Images using Terminal on Linux with ImageMagick

ImageMagick is a free command-line software for manipulating digital images. And one of its features is compression. It can deal with a lot of formats which include JPEG.

First, let’s install ImageMagick with this command:

$ sudo apt install imagemagick

Output:

install imagmagick linux

ImageMagick comes with the command convert which you can use to reduce the image quality. You input the target file, the amount of quality to reduce, and the name of the output file. So an example command would look like this:

$ convert butterfly.jpg -quality 20% butterfly_converted.jpg

Output:

compress image

This is the current size of the image.

compressed image size linux

And this is the resolution compared to the original.

compressed image

You can compare both images to understand the change using this command. Another way to use the convert command is to specify pixels instead of quality percentages. The command looks like this:

$ convert butterfly.jpg -resize 500x500! butterfly_converted.jpg

Output:

Compress JPEG Images using Terminal on Linux
compressed image pixels linux

This not only reduced the resolution but also the size of the image.

compressed image size linux

Another useful command in the ImageMagick tools is mogrify. Now, it has very similar operations as convert. The main difference is that ‘convert’ creates a new file while ‘mogrify’ does the operation on the original file.

To compress an image using mogrify, use this command:

$  mogrify -quality 10 butterfly.jpg

Output:

Compress JPEG Images using Terminal on Linux

The size has been also reduced immensely, as you can see in the below screenshot:

compressed image size linux

You can also batch compress with this command, like this:

$ mogrify -quality 10 *.jpg

You can learn more about ImageMagick and other its other useful features on the official website.

Final Thoughts

This guide shows you the different tools and methods to compress JPEG images using Terminal on Linux. We tried to show you all available tools, the good side of each, and how to best utilize them when compressing images. That’s it for this tutorial. If you have confusions, feel free to let us know in the comments below.

If this guide helped you, please share it.

Related Posts