Linux Commands

Compress JPEG Images on Linux Using Terminal

In Linux JPEG compression is a method for reducing the file size of digital images while maintaining their quality. The Linux terminal offers several tools for compressing JPEG images, such as the “jpegoptim” command. Using this tool, we can optimize our images for different usage by reducing their size without losing image quality.

Note: Here we have used a JPG format image however both the JPEG and JPG are the same. This tool can convert any of them.

How to Install jpegoptim on Linux

To install the jpegoptim tool run the following command:

$ sudo apt-get install jpegoptim

We have a test image with a size of 3.1MB. Now we will compress this image using the jpegoptim tool.

We can also check image size using the list command:

$ ls -l

Compressing Image Using jpegoptim to an Optimize Value

To compress the image run following command:

$ jpegoptim image.jpg

Here we can see the image is compressed slightly by 0.23%. This is auto compression by jpegoptim tool.

However, we can also define the size at which we want to compress the image.

Compressing Image to a Specific Size

Run the below command to compress the image from 3mb to approximately 1mb file. You can also define a custom file size.

$ jpegoptim --size=1000k image.jpg

Terminal shows image size reduced from 3mb to 1mb.

Compressing and Save Image to a Specific Destination

To compress image to a specific destination run given command:

$ jpegoptim --size=800k image.jpg --dest ~/Desktop

This command will reduce image size to approximately 800 kb and save the file on Desktop.

To confirm the image conversion, we can list the Desktop files:

$ ls -l ~/Desktop

Optimizing Several Images at a Time

To optimize multiple images all at once run the given command:

$ jpegoptim image1.jpg image2.jpg image3.jpg

Batch Process an Entire Folder of Images

To batch process an entire folder of jpg images run the command:

$ jpegoptim ~/Pictures/image/*.jpg

To find further information and full list of option about jpegoptim tool run the given command:

$ man jpegoptim

Conclusion

The JPEG compression in Linux can be done using the jpegoptim tool in the terminal. This provides a convenient and efficient way to optimize JPEG images for web usage, reducing file size while maintaining image quality.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.