How to Optimize and Compress JPEG or PNG Images on CentOS/RHEL 7, Ubuntu 19.10 and Debian 9 - Cloud Network

Networking | Support | Tricks | Troubleshoot | Tips

Buymecoffe

Buy Me A Coffee

Monday, October 21, 2019

How to Optimize and Compress JPEG or PNG Images on CentOS/RHEL 7, Ubuntu 19.10 and Debian 9

 





 Compress or Optimize JPEG Images from Command Line


jpegoptim is a command line tool that can be used to optimize and compress JPEG, JPG and JFIF files without losing its actual quality.

This tool supports lossless optimization, which is based on optimizing the Huffman tables.

Install jpegoptim in RedHat Linux, Fedora & CentOS based Systems

On RPM based systems like RHEL, CentOS, Fedora etc.,
You need to install and enable EPEL repository or alternatively,
You can install the epel repository directly from the command line as shown:

Step1:-  yum install epel-release  [Redhat Linux, Fedora & CentOS]
Step1:-  dnf install epel-release  [On Fedora 22+ Versions]

Install jpegoptim in Ubuntu, Linux Mint & Debian based Systems

Step2:-  sudo apt-get install ncdu [Ubuntu, Linux Mint & Debian]

Next install jpegoptim program from the repository
Step3:- yum install jpegoptim [ On Redhat Linux, CentOS]
Step4:- dnf install jpegoptim [On Fedora 22+ versions]

How to Use Jpegoptim  Image  Optimizer
Step5:- jpegoptim cloud.jpeg
Step6:- jpegoptim [options] cloud.jpeg

Let’s now compress the following cloud.jpeg image, but before optimizing the image, first find out the actual size of the image using du command as shown.
Step7:- du -sh cloud.jpeg

Here the actual file size is 6.2MB, now compress this file by running:
Step8:-  jpegoptim cloud.jpeg

Open the compressed image in any image viewer application, you will not find any major differences. The source and compressed images will have the same quality.

The above command optimizes the images to the maximum possible size. However, you can compress the given image to a specific size to, but it disables the lossless optimization.

For example, let us compress above the image from 5.6MB to around 250k.

Step9:- jpegoptim --size=250k  cloud.jpeg

Batch JPEG Image Compression and Optimization
Step10:- cd img/
Step11:- ls -l

And then run the following command to compress all images at once.
Step12:- jpegoptim *.JPG

You can also compress multiple selected images at once:
Step13:- jpegoptim  DSC_0310.JPG  DSC_0311.JPG  DSC_0312.JPG


For more details about jpegoptim tool, check out the man pages.
Step14:- man jpegoptim

Compress or Optimize PNG Images from Command Line

OptiPNG is a command line tool used to optimize and compress PNG
(portable network graphics) files without losing its original quality.

The installation and usage of OptiPNG is very similar to jpegoptim.

Install optipng in RedHat Linux, Fedora & CentOS based Systems

On RPM based systems like RHEL, CentOS, Fedora etc.,
You need to install and enable EPEL repository or alternatively,
You can install the epel repository directly from the command line as shown:

Step15:-  yum install optipng [Redhat Linux, Fedora & CentOS]
Step16:-  dnf install optipng [On Fedora 22+ Versions]

Install optipng in Ubuntu, Linux Mint & Debian based Systems
Step17:-  sudo apt-get install optipng [Ubuntu, Linux Mint & Debian]

How to Use OptiPNG Image Optimizer
Step18:- optipng cloud.png
Step19:- optipng [options] cloud.png

Let us compress the cloud.png image, but before optimizing, first check the actual size of the image as shown:

Step20:- ls -lh cloud.png

Here the actual file size of above image is 350K, now compress this file by running:
Step21:- optipng cloud.png

Step22:- ls -lh cloud.png

Open the compressed image in any image viewer application, you will not find any major differences between the original and compressed files. The source and compressed images will have the same quality.

Batch PNG Image Compression and Optimization

To compress batch or multiple PNG images at once, just go the directory where all images resides and run the following command to compress.

Step23:- cd img/
Step24:- optipng *.png

For more details about optipng check man pages.
Step25:- man optipng

If you’re a webmaster and wants to serve optimized images over your website or a blog, these tools can be very handy.

These tools not only saves the disk space, but also the reduces the bandwidth while uploading the images.