Skip to content
Related Articles

Related Articles

Optimize and Compress JPEG or PNG Images in Linux Command line

Improve Article
Save Article
  • Last Updated : 31 Aug, 2021
Improve Article
Save Article

Today there are many GUI tools that compress and optimize the images, which are very useful. But if you are terminal addicted and want to compress and optimize the images in the terminal before uploading to the cloud, then you can do it. There are two tools by using which we can compress and optimize images. These are –

  • jpegoptim
  • OptiPNG


jpegoptim is a command-line utility to optimize and compress JPEG/JFIF and JPG files. This utility support lossless optimization which is based on optimizing the Huffman tables. Now let’s see how to install the jpegoptim on different Linux distributions.


The jpegoptim tool is available to install on most of the Linux package managers. Use one of the following commands according to your operating system to install jpegoptim utility.

For Ubuntu/Debian/Kali Linux:

apt-get install jpegoptim

For Alpine:

apk add jpegoptim

For Arch Linux:

pacman -S jpegoptim

For Fedora:

dnf install jpegoptim

For OSX:

brew install jpegoptim

How to use Jpegoptim Image Optimizer

The syntax of jpegoptim is very simple to use but note that the jpegoptim compresses the file and replaces them with original file and to avoid this we need to mention the directory after jpegoptim command we will see later how to do that. To use the jpegoptim utility for one file, just mention the filename after the jpegoptim command, like:

jpegoptim  gfg.jpeg

Now let’s take one example we are going to compress an image named gfg.jpg but first of all, let’s see what is the actual size of that image. We can use simple ls  command to get the size of the image:

ls -l gfg.jpg

We can see the size of the gfg.jpg image is 5.3 MB. Now let’s use jpegoptim utility on that image:

The size is changed by just 200 KB, this is because this tool does not lose the quality of the image while compressing.

Image pre-compression simulation

Instead of actually compressing the file, if you want to simulate the compression of JPEG files and see what will be the size of the image after reducing then use option -n with the jpegoptim command:

jpegoptim  -n gfg.jpg

Compressing Image to a fixed size using jpegoptim

We can compress the image to a fixed size which we want, for that we have to use the –size option with jpegoptim command and mention the size of the image that we want after compressing. Let’s compress the same gfg.jpg file to 200k using jpegoptim with –size option.

jpegoptim --size=200k gfg.jpg

Batch JPEG Image Compression & Optimization

We can also compress all .jpg files in the directory. To compress all .jpg files in the current directory, use the following command:

jpegoptim  *.jpg

To compress selective files in the directory, just mention the image names separated by space after jpegoptim  command:

jpegoptim  gfg_1.jpg gfg_2.jpg gfg_3.jpg

To save the output of jpegoptim  in other folder use option -d and mention the folder name:

 jpegoptim  -d foldername/ image.jpg

Here is one example:

jpegoptim  -d ./compressed/ *.jpg

To know more about jpegoptim read the man page of jpegoptim  using the man command:

man jpegoptim 


OptiPNG is a command-line tool that compresses portable network graphics (PNG) files without losing semantic information. Now let’s see how to install OptiPNG on different Linux distributions and OSX. Use one of the following commands according to Your operating system:

For Debian/Ubuntu/Kali Linux:

apt-get install optipng

For Alpine:

apk add optipng

For Arch Linux:

pacman -S optipng

For CentOS:

yum install optipng

For Fedora:

dnf install optipng

For OSX:

brew install optipng

Using OptiPNG Image Optimizer

The syntax of using OptiPng is very simple, we have to just mention the name of the png file after the optipng command.

optipng gfg.png

Let’s compress the file named gfg.png before that, find out the size of the gfg.png file using the ls command:

ls -l gfg.png

The size of gfg.png is 534k now let’s use the optipng tool on that file

optipng gfg.png

We can see that the size gets reduced from 534k to 522k.

Batch PNG Image Compression & Optimization

We can optimize the all .png files in a folder using the following command:

optipng *.png

To save generated output files into another folder, use the -dir option with optipng command:

optipng --dir=compressed gfg.png

To compress selective files with the optipng just mention the file names separated by space:

optipng gfg1.png gfg2.png

To know more about the optipng command, read the man page of optipng using man command:

man optipng

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!