How To Optimize And Compress Jpeg Or Png Images In Linux Terminal

How To Optimize And Compress Jpeg Or Png Images In Linux Terminal

jpegoptim and optipngthese are two tools for optimising and reducing image size in linux terminal. These tool are very useful when you are serving images on website. This will help you to reduce load time for static content in website. So lets start with jpegoptim, first of all we will install jpegoptim in our system. Contents of this tutorial are:

Installing Jpegoptim in Linux

To install jpegoptim in debian just simply use this command :

$ apt install jpegoptim
  # or with non root user type
$ sudo apt install jpegoptim

Use jpegoptim as Image Optimiser

To use jpegoptim as image optimiser navigate to directory where your jpeg/jpg file is present and type the following command,

$ jpegoptim yourfilename.jpeg
  # or
$ jpegoptim yourfilename.jpg 

in my case i have filename test.jpeg so the corresponding output after running that command will be

$  jpegoptim test.jpeg
  # and output is
$ test.jpeg 2560x1600 24bit N JFIF  [OK] 689488 --> 551590.4 bytes (20.00%), skipped.

so as you can see in above output test.jpeg file size is reduced by 20% of its overall size, this will reduce loading time in web pages. Let us take a scenario in which we have 'x' numbers of jpeg image in our directory to optimise size of all image at once we will use wild card to select all image and optimise its size using jpegoptim, for example

$ cd your_image_directory
$ jpegoptim *.jpeg
 # here in above command we have used wildcard to select and optimise all the image at once irrespective of their file names

you can learn more about jpegoptim by reading its manual, to open manual page in linux simply type man jpegoptim inside a terminal.

Installing Optipng in Linux

To install optipng in debian just simply use this command :

$ apt install optipng
  # or with non root user type
$ sudo apt install optipng

Use optipng as Image Optimiser

To use jpegoptim as image optimiser navigate to directory where your .png file is present and type the following command,

$ jpegoptim yourfilename.png

In my case file which we have to optimise is testfile.png, so the corresponding output after running command will be

$  optipng filetest.png
  # and output is
** Processing: filetest.png
972x533 pixels, 4x8 bits/pixel, RGB+alpha
Reducing image to 3x8 bits/pixel, RGB
Input IDAT size = 68328 bytes
Input file size = 68497 bytes

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

Output IDAT size = 48999 bytes (19329 bytes decrease)
Output file size = 49071 bytes (19426 bytes = 28.36% decrease)

so, here you can see that optipng has decreased overall size of image by 28.36%. you can learn more about optimpng by reading its manual by typing command man optimpng in teriminal.

Leave a Comment