How to compress JPEG images on the command line under Linux system

Source: Internet
Author: User

Today, photographic devices, such as smartphones and digital cameras, are getting bigger resolution. Even the 36.3 million-megapixel Nikon D800 has rushed into the market, and this trend simply can't stop. Today's camera devices are constantly improving the resolution of the photos, so we have to compress them and upload them to the cloud with storage limits and bandwidth limits.

In fact, there's a very simple way to compress JPEG images. A "jpegoptim" command-line tool can help you "lossless" the JPEG image so that you can compress JPEG images without sacrificing their quality. In case your storage space and bandwidth budget are really small, Jpegoptim also supports lossy compression to resize the image.

If you want to compress the PNG image, refer to the example in this guide.

Install Jpegoptim

Ubuntu, Debian or Linux Mint:

The code is as follows:

$ sudo apt-get install Jpegoptim


The code is as follows:

$ sudo yum install Jpegoptim

Centos/rhel installation, first open the Epel library, and then run the following command:

The code is as follows:

$ sudo yum install Jpegoptim

Lossless JPEG image compression

In order to compress a pair of JPG images lossless, use:

The code is as follows:

$ Jpegoptim photo.jpg

Photo.jpg 2048x1536 24bit N ICC JFIF [OK] 882178--> 821064 bytes (6.93%), optimized.

Note that the original image will be overwritten by the compressed image.

If the Jpegoptim does not lossless the image, it will not be overwritten:

The code is as follows:

$ jpegoptim-v photo.jpg

Photo.jpg 2048x1536 24bit N ICC JFIF [OK] 821064--> 821064 bytes (0.00%), skipped.

If you want to protect the original picture, use the "-D" parameter to indicate the save directory

The code is as follows:

$ jpegoptim-d./compressed photo.jpg

In this way, the compressed picture will be saved in the./compressed directory (with the same input filename)

If you want to protect the creation time of the file, use the "-P" parameter. This compressed image will get the same date time as the original picture.

The code is as follows:

$ jpegoptim-d./compressed-p photo.jpg

If you just want to see lossless compression rather than really trying to compress them, use the "-n" parameter to simulate the compression, and then it will show the compression rate.

The code is as follows:

$ jpegoptim-n photo.jpg

lossy compression jpg image

In case you really need to keep it in the cloud space, you can also use lossy compression jpg images.

In this case, use the "-m< quality >" option, nuclei range 0 to 100. (0 is the best quality, 100 is the worst quality)

For example, compress pictures with 50% quality:

The code is as follows:

$ JPEGOPTIM-M50 photo.jpg

Photo.jpg 2048x1536 24bit N ICC JFIF [OK] 882178--> 301780 bytes (65.79%), optimized.

On the basis of sacrificing quality, a smaller picture will be obtained.

Compress more than one JPEG image at a time

The most common scenario is the need to compress multiple JPEG image files in one directory. To cope with this situation, you can use the next script.

The code is as follows:


# compress all *.jpg files in current directory

# saved in./compressed Directory

# and have the same modification date as the original file

For i in *.jpg; Do jpegoptim-d/compressed-p "$i"; Done

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.