Batch Compress JPEG Lossless Images Using Command Line on Linux

Overall page size is a critical factor for a fast website. Normal page size is large for your image site. There are various WordPress plugins for batch optimization of JPEG and PNG images such as TinyJPG and Ewww… This tutorial will show you how to losslessly compress all JPEG images for your site using the Linux command line without compromising on quality.

After that, you can easily and without bugs use Photoshop online and edit photos in any format.

Batch Compress JPEG Lossless Images Using Command Line on Linux

overview

  • Install jpegoptim
    • From the repository
    • Compiling latest jpegoptim
  • Compressing single images
  • Batch compression of all images

Installing jpegoptim from the repository

sudo apt-get update
sudo apt-get install jpegoptim -y

Check your current jpegoptim version

jpegoptim -V

We need the latest version to improve performance, so we will compile the latest jpegoptim from source

jpegoptim v1.4.3  x86_64-pc-linux-gnu
Copyright (c) 1996-2015, Timo Kokkonen

libjpeg version: 8d  15-Jan-2012
Copyright (C) 1991-2015 The libjpeg-turbo Project and many others

Compiling jpegoptim

Install Jpeg library and compilation tools

sudo apt-get install libjpeg-dev build-essential

Download the latest source code which can be found here, build and install it

cd /tmp
wget http://www.kokkonen.net/tjko/src/jpegoptim-1.4.4.tar.gz
tar -xvf jpegoptim*
cd jpegoptim*
./configure --prefix=/usr
make
sudo make install

Check jpegoptim version again

jpegoptim -V

We are now on the latest and greatest version of jpegoptim

jpegoptim v1.4.4  x86_64-unknown-linux-gnu
Copyright (c) 1996-2016, Timo Kokkonen

libjpeg version: 8d  15-Jan-2012
Copyright (C) 1991-2015 The libjpeg-turbo Project and many others

Compressing single images

Lossless compression means there is no loss of quality, so the original JPG is overwritten

jpegoptim image.jpg

You will receive a small report on how the compression went.

image.jpg 1364x1024 24bit N Exif ICC XMP IPTC Adobe JFIF  [OK] 1210895 --> 1165543 bytes (3.75%), optimized.

If you want to remove EXIF ​​and comment metadata, you can use the flag – -strip-al.

jpegoptim --strip-all image.jpg

Batch Compression JPEGs

We recommend that you get the folder size first, before running the command to optimize

du -sh foldername

We now have the size of the directory to be compressed

212M

If you have thousands of images, it is a good idea to use the screen to run the optimization.

The screen will ensure the batch conversion command to work even if your SSH session ends.

sudo apt-get install screen

Create a new session screen, press the spacebar, or type screen on the screen.

screen

Now you can run this command which will find and compress each image in each subfolder recursively.

find /var/www/andreyex.ru -type f -iname '*.jpg' -exec jpegoptim --strip-all {} +

Detach the screen with Ctrl + A and press the D (undock) button.

You can use the Top command and look at the processes jpegoptim at the same time.

Install screen like so

screen -r

Check the folder size again

du -sh foldername

Saved about 15 MB?

206M

The savings can be even greater with lossy compression, which you can check with this guide.

Sidebar