Skip to content

Instantly share code, notes, and snippets.

@TopekoX
Created April 4, 2022 08:46
Show Gist options
  • Save TopekoX/9ba1a1ee14af9398019204228b08b8d6 to your computer and use it in GitHub Desktop.
Save TopekoX/9ba1a1ee14af9398019204228b08b8d6 to your computer and use it in GitHub Desktop.
Compress JPEG or PNG Images in Linux Commandline

Install jpegoptim in Linux

To install jpegoptim on your Linux systems, run the following command from your terminal.

On Debian and it’s Derivatives

# apt-get install jpegoptim

or

$ sudo apt-get install jpegoptim

On RedHat 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 commandline as shown:

# yum install epel-release
# dnf install epel-release    [On Fedora 22+ versions]

Next install jpegoptim program from the repository as shown:

# yum install jpegoptim
# dnf install jpegoptim    [On Fedora 22+ versions]

How to Use Jpegoptim Image Optimizer

The syntax of jpegoptm is:

$ jpegoptim filename.jpeg
$ jpegoptim [options] filename.jpeg

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

$ du -sh image.jpeg 

6.2M	image.jpeg

Here the actual file size is 6.2MB, now compress this file by running:

$ jpegoptim image.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.

$ jpegoptim --size=250k image.jpeg

Batch JPEG Image Compression and Optimization

You might ask how to compress the images in the entire directory, that’s not difficult too. Go to the directory where you have the images.

ls -l
total 812
-rw-rw-r-- 1 ucup ucup  75147 Apr  3 12:52 environment-windows-path.jpg
-rw-rw-r-- 1 ucup ucup 109007 Apr  3 12:52 index.png
-rw-rw-r-- 1 ucup ucup  72571 Apr  3 12:52 install-jdk-1.png
-rw-rw-r-- 1 ucup ucup  69421 Apr  3 12:52 install-jdk-2.png
-rw-rw-r-- 1 ucup ucup  65775 Apr  3 12:52 isi-jdk.png
-rw-rw-r-- 1 ucup ucup  17144 Apr  3 12:52 javac-version-win.jpg
-rw-rw-r-- 1 ucup ucup   8299 Apr  3 12:52 java-logo.png
-rw-rw-r-- 1 ucup ucup  86090 Apr  3 12:52 java-version.png
-rw-rw-r-- 1 ucup ucup  36875 Apr  3 12:52 java-version-win.jpg
-rw-rw-r-- 1 ucup ucup   4893 Apr  3 12:52 jdk.png
-rw-rw-r-- 1 ucup ucup   4169 Apr  3 12:52 jre.png
-rw-rw-r-- 1 ucup ucup  30815 Apr  3 12:52 linux-javac.png
-rw-rw-r-- 1 ucup ucup  47799 Apr  3 12:52 linux-java.png
-rw-rw-r-- 1 ucup ucup  69075 Apr  3 12:52 mac-javac.png
-rw-rw-r-- 1 ucup ucup  80540 Apr  3 12:52 mac-java.png
-rw-rw-r-- 1 ucup ucup  12732 Apr  3 12:52 proses-java.png

And then run the following command to compress all images at once.

jpegoptim *.jpg

You can also compress multiple selected images at once:

jpegoptim image1.JPG image2.JPG image3.JPG 

Install OptiPNG in Linux

To install OptiPNG on your Linux systems, run the following command from your terminal.

On Debian and it’s Derivatives

# apt-get install optipng

or

$ sudo apt-get install optipng

On RedHat based Systems

# yum install optipng
# dnf install optipng    [On Fedora 22+ versions]

Note: You must have epel repository enabled on your RHEL/CentOS based systems to install optipng program.

How to Use OptiPNG Image Optimizer The general syntax of optipng is:

$ optipng filename.png
$ optipng [options] filename.png

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

ls -l Screenshot_20220404_022545.png
-rw-rw-r-- 1 ucup ucup 233879 Apr  4 02:25 Screenshot_20220404_022545.png

Here the actual file size of above image is 190K, now compress this file by running:

$ optipng Screenshot_20220404_022545.png
** Processing: Screenshot_20220404_022545.png
1530x1130 pixels, 4x8 bits/pixel, RGB+alpha
Input IDAT size = 233465 bytes
Input file size = 233879 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0         IDAT size = 195263
                               
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0         IDAT size = 195263

Output IDAT size = 195263 bytes (38202 bytes decrease)
Output file size = 195341 bytes (38538 bytes = 16.48% decrease)

As you see in the above output, the size of the Screenshot_20220404_022545.png file has been reduced up to 16.48%. Now verify the file size again using:

ls -l Screenshot_20220404_022545.png
-rw-rw-r-- 1 ucup ucup 195341 Apr  4 16:38 Screenshot_20220404_022545.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.

$ optipng *.png
** Processing: Screenshot_20220402_015449.png
1410x826 pixels, 4x8 bits/pixel, RGB+alpha
Input IDAT size = 161549 bytes
Input file size = 161855 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0         IDAT size = 141863
                               
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0         IDAT size = 141863

Output IDAT size = 141863 bytes (19686 bytes decrease)
Output file size = 141941 bytes (19914 bytes = 12.30% decrease)

** Processing: Screenshot_20220404_011745.png
1027x662 pixels, 4x8 bits/pixel, RGB+alpha
Input IDAT size = 230496 bytes
Input file size = 237138 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0         IDAT size = 212218
                               
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0         IDAT size = 212218

Output IDAT size = 212218 bytes (18278 bytes decrease)
Output file size = 218524 bytes (18614 bytes = 7.85% decrease)

** Processing: Screenshot_20220404_021939.png
1761x1069 pixels, 4x8 bits/pixel, RGB+alpha
Input IDAT size = 240338 bytes
Input file size = 240764 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0         IDAT size = 214004
                               
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0         IDAT size = 214004

Output IDAT size = 214004 bytes (26334 bytes decrease)
Output file size = 214082 bytes (26682 bytes = 11.08% decrease)

** Processing: Screenshot_20220404_022545.png
1530x1130 pixels, 4x8 bits/pixel, RGB+alpha
Input IDAT size = 195263 bytes
Input file size = 195341 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0         IDAT size = 195263
                               
Screenshot_20220404_022545.png is already optimized.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment