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
Fedora:
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:
#!/bin/sh
# 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