How to optimize and compress jpeg or png images in linux


Do you want to optimize the images or compress images without losing its original quality before uploading them to any cloud or local storages? There are plenty of GUI applications or web applications which are available that will help you to optimize the images. This article helps you to optimize and compress JPEG or PNG images in Linux Command Line.

Using jpegoptim

jpegoptim is a simple utility to optimize JPEG images without losing quality. It provides lossless optimization and “lossy” optimization based on the setting with a maximum quality factor.

To install jpegoptim , use the following command –

$ sudo apt-get install jpegoptim

The sample output should be like this –

The following packages were automatically installed and are no longer required:
gcc-4.8-base:i386 libasn1-8-heimdal:i386 libasound2:i386
libasound2-plugins:i386 libasyncns0:i386 libavahi-client3:i386
libavahi-common-data:i386 libavahi-common3:i386 libbit-vector-perl
libcapi20-3:i386 libcarp-clan-perl libclass-method-modifiers-perl
libcups2:i386 libdata-random-perl libdate-calc-perl libdate-calc-xs-perl
libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386
libdrm-radeon1:i386 libedit2:i386 libelf1:i386 libexif12:i386 libexpat1:i386
libffi6:i386 libflac8:i386 libfontconfig1:i386 libfreetype6:i386
libgcrypt11:i386 libgd-perl libgd3:i386 libgif4:i386
libgl1-mesa-dri-lts-wily:i386 libgl1-mesa-glx-lts-wily:i386
libglapi-mesa-lts-wily:i386 libglib2.0-0:i386 libglu1-mesa:i386
libgnome2-gconf-perl libgnutls26:i386 libgpg-error0:i386 libgphoto2-6:i386
libgphoto2-port10:i386 libgssapi-krb5-2:i386 libgssapi3-heimdal:i386
libgstreamer-plugins-base0.10-0:i386 libgstreamer0.10-0:i386
libhcrypto4-heimdal:i386 libheimbase1-heimdal:i386 libheimntlm0-heimdal:i386
libhx509-5-heimdal:i386 libice6:i386 libieee1284-3:i386
libjack-jackd2-0:i386 libjbig0:i386 libjpeg-turbo8:i386 libjpeg8:i386
libk5crypto3:i386 libkeyutils1:i386 libkrb5-26-heimdal:i386 libkrb5-3:i386
libkrb5support0:i386 liblcms2-2:i386 libldap-2.4-2:i386 libllvm3.6:i386
libltdl7:i386 libmouse-perl libmpg123-0:i386 libnet-dropbox-api-perl
libogg0:i386 libopenal1:i386 liborc-0.4-0:i386 libosmesa6:i386
libp11-kit-gnome-keyring:i386 libp11-kit0:i386 libpciaccess0:i386
libpulse0:i386 libroken18-heimdal:i386 libsamplerate0:i386 libsane:i386
libsasl2-2:i386 libsasl2-modules:i386 libsasl2-modules-db:i386 libsm6:i386
libsndfile1:i386 libspeexdsp1:i386 libsqlite3-0:i386 libssl1.0.0:i386
libstdc++6:i386 libtasn1-6:i386 libtiff5:i386 libtxc-dxtn-s2tc0:i386
.........................

Using Jpegoptim Image Optimizer

Use the following command to optimize the tp1.jpg file-

$ jpegoptim /home/linux/Desktop/tp1.jpg

The sample output should be like this –

/home/linux/Desktop/tp1.jpg 1024x744 24bit N IPTC Adobe JFIF [OK] 535222 --> 508663 bytes (4.96%), optimized.

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

For example, compress the above image to around 250K, Use the following command –

$ jpegoptim --size=250k /home/linux/Desktop/tp1.jpg

The sample output should be like this –

/home/linux/Desktop/tp1.jpg 1024x744 24bit N IPTC JFIF [OK] 508663 --> 249866 bytes (50.88%), optimized.

To find the file size, use the following command-

$ du -sh /home/linux/Desktop/tp1.jpg

The sample output should be like this –

248K/home/linux/Desktop/tp1.jpg

The jpegoptim command is compressed to an image size of 248k. If required, you can change the size of images.

Batch JPEG Image Compression and Optimization

jpegoptim can compress batch images, now select the folder of images which you can compress as shown below-

$ cd /home/linux/Desktop/img
/Desktop/img$ ls -l

The above command shows the list of all images as shown below –

total 2188
-rw-rw-r-- 1 linux linux 32616 Feb 27 10:46 Converted_file_1a46a9ef.jpg
-rw-rw-r-- 1 linux linux 35925 Feb 27 10:46 Converted_file_96eba41d.jpg
-rw-rw-r-- 1 linux linux 50600 Feb 27 10:46 Converted_file_dd3307c9.jpg
-rw-rw-r-- 1 linux linux 5083 Feb 27 10:48 LOGO122.png
-rw-rw-r-- 1 linux linux 2047570 Feb 27 12:39 nexus_6_0134.jpg
-rw-rw-r-- 1 linux linux 36364 Feb 27 10:45 unnamed.jpg

You can then, run the following command to compress all JPEG images at once.

/Desktop/img$ jpegoptim --size=250k *.jpg

The sample output should be like this –

Converted_file_1a46a9ef.jpg 480x800 24bit N JFIF [OK] 32616 --> 32616 bytes (0.00%), skipped.
Converted_file_96eba41d.jpg 480x800 24bit N JFIF [OK] 35925 --> 35925 bytes (0.00%), skipped.
Converted_file_dd3307c9.jpg 480x800 24bit N JFIF [OK] 50600 --> 50600 bytes (0.00%), skipped.
nexus_6_0134.jpg 1607x2896 24bit N Exif IPTC ICC JFIF [OK] 1889349 --> 260524 bytes (86.21%), optimized.
unnamed.jpg 480x800 24bit N JFIF [OK] 36364 --> 24816 bytes (31.76%), optimized.

In the above result, it has compressed JPG files into 250K sized files.

To compress the selected files, use the following command –

jpegoptim file_name.jpg file_name1.jpg file_name2.jpg

For more details about jpegoptim, use the following command-

$ man jpegoptim

The sample output should be like this –

NAME
   jpegoptim - utility to optimize/compress JPEG/JFIF files.
SYNOPSIS
   jpegoptim [ options ] [ filenames ]
DESCRIPTION
   jpegoptim is used to optimize/compress jpeg files. Program supports lossless optimization, which is based    on optimizing the Huffman tables. And so called "lossy" optimization where in addition to optimizing
   Huffman tables user can specify upperlimit for image quality.
OPTIONS
   Options may be either the traditional POSIX one letter options, or the GNU style long options. POSIX style    options start with a single ``-'', while GNU long options start with ``--''.
   Options offered by jpegoptim are the following:
   -d, --dest=
.....................

Optimizing PNG images

OptiPNG is a simple utility to optimize PNG images without losing any information. This program also converts external formats (BMP, GIF, PNM and TIFF) to optimized PNG, and performs PNG integrity checks and corrections.

To install optiPNG, use the following command –

$ sudo apt-get install optiPNG

The sample output should be like this –

Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gcc-4.8-base:i386 libasn1-8-heimdal:i386 libasound2:i386
libasound2-plugins:i386 libasyncns0:i386 libavahi-client3:i386
libavahi-common-data:i386 libavahi-common3:i386 libbit-vector-perl
libcapi20-3:i386 libcarp-clan-perl libclass-method-modifiers-perl
libcups2:i386 libdata-random-perl libdate-calc-perl libdate-calc-xs-perl
libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386
libdrm-radeon1:i386 libedit2:i386 libelf1:i386 libexif12:i386 libexpat1:i386
libffi6:i386 libflac8:i386 libfontconfig1:i386 libfreetype6:i386
libgcrypt11:i386 libgd-perl libgd3:i386 libgif4:i386
libgl1-mesa-dri-lts-wily:i386 libgl1-mesa-glx-lts-wily:i386
libglapi-mesa-lts-wily:i386 libglib2.0-0:i386 libglu1-mesa:i386
libgnome2-gconf-perl libgnutls26:i386 libgpg-error0:i386 libgphoto2-6:i386
.........................................

To check the actual size of PNG, use the following command

$ ls -lh /home/linux/Desktop/logo.png

The sample output should be like this –

-rw-rw-r-- 1 linux linux 3.4M Mar 3 12:05 /home/linux/Desktop/logo.png

Here the actual file size of above image is 3.4MB, now compress this file by running the following command –

$ optipng /home/linux/Desktop/logo.png

The sample output should be like this –

Copyright (C) 2001-2010 Cosmin Truta.
** Processing: /home/linux/Desktop/logo.png
2560x1920 pixels, 3x8 bits/pixel, RGB
Input IDAT size = 3506320 bytes
Input file size = 3511513 bytes
Trying:
zc = 9 zm = 8 zs = 0 f = 5IDAT size = 3260520
zc = 9 zm = 8 zs = 1 f = 5IDAT size = 3257566

Selecting parameters:
zc = 9 zm = 8 zs = 1 f = 5IDAT size = 3257566
Output IDAT size = 3257566 bytes (248754 bytes decrease)
Output file size = 3257623 bytes (253890 bytes = 7.23% decrease)

Now check the size of log.png. Use the following command-

$ ls -lh /home/linux/Desktop/logo.png

The sample output should be like this –

-rw-rw-r-- 1 linux linux 3.2M Mar 3 12:07 /home/linux/Desktop/logo.png

As you see in the above output, the size of the tecmint.png file has been reduced up to 7.23%.Open the compressed image in any image viewer application, you will not find any major differences between the original and compressed files.

Batch PNG Image Compression and Optimization

To compress batch or multiple PNG images at once, Use the following commands –

$ cd /home/linux/Desktop/img/
~/Desktop/img$ optipng *.png

The sample output should be like this –

Copyright (C) 2001-2010 Cosmin Truta.
** Processing: LOGO122.png
149x90 pixels, 4x8 bits/pixel, RGB+alpha
Input IDAT size = 5026 bytes
Input file size = 5083 bytes
Trying:
zc = 9 zm = 8 zs = 0 f = 0IDAT size = 4934

Selecting parameters:
zc = 9 zm = 8 zs = 0 f = 0IDAT size = 4934
Output IDAT size = 4934 bytes (92 bytes decrease)
Output file size = 4991 bytes (92 bytes = 1.81% decrease)

For more details about optipng, use the following command –

$ man optipng

The sample output should be like this –

NAME
   OptiPNG - Advanced optimization program for Portable Network Graphics (PNG)
SYNOPSIS
   optipng [-? | -h | -help]
   optipng [options...] files...
DESCRIPTION
   The OptiPNG program shall attempt to optimize PNG files, i.e. reduce their size to a minimum, without          losing any information. In addition, this program shall perform a suite of auxiliary functions like
      integrity checks, metadata recovery and pixmap-to-PNG conversion.
   The optimization attempts are not guaranteed to succeed. Valid PNG files that cannot be optimized by this    program are normally left intact; their size will not grow. The user may request to override this
   default behavior.
................................

Congratulations! Now, you know “How to Optimize and Compress JPEG or PNG Images in Linux Command Line”. We’ll learn more about these types of commands in our next Linux post. Keep reading!

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 21-Oct-2019

293 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements