Batch Compress JPEG Images Lossless Linux Command Line

Overall page size is a critical factor for a fast website especially for mobile users. The usual suspect for making page size large for your site is the images. There are various WordPress plugins for batch optimizing jpeg and png images like TinyJPG and EWWW. This tutorial shows you how to losslessly compress all of the JPEG images for your site using the Linux command line so no quality is lost.

I used a Vultr VPS running Ubuntu 14.04 for this tutorial.

Batch Compress JPEG Images Lossless Linux Command Line

Overview

  • Installation of jpegoptim
    • from repository
    • Compiling the latest jpegoptim
  • Compressing single images
  • Batch compressing all images

Install jpegoptim from repository

Update repository list and install jpegoptim

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

Check the current version of jpegoptim

jpegoptim -V

It is 2017 and I like to have the latest version for performance reasons 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

Compile Latest jpegoptim

Grab the 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

Chech the jpegoptim version again

jpegoptim -V

Now we are on the latest and greatest 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 quality loss so the original JPG is overwritten

jpegoptim image.jpg

You get a little report about how compression went

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

If you want to strip the exif and comment metadata you can use the –-strip-all flag

jpegoptim --strip-all image.jpg

Batch Compressing JPEGs

I recommend getting the folder size first before running the command to batch optimize

du -sh foldername

Now we have a baseline

412M

If you having thousands of images, it’s a good idea to use screen to run the optimizations.

Screen will ensure the batch command keeps on running even if your SSH session is terminated.

sudo apt-get install screen

Create a new screen session, press space or enter at the intro screen

screen

Now you can run this find command which will compress every image in every subfolder recursively

find /var/www/wp-bullet.com -type f -iname '*.jpg' -exec jpegoptim --strip-all {} +

Detach the screen with Ctrl+A and pressing D (detach).

You can use the top command and look for jpegoptim processes once in a while.

Reattach the screen like so

screen -r

Check the folder size again

du -sh foldername

Saved about 10 MB 🙂

401M

The savings can be even greater with lossy compression which you can test with this tutorial.

Sources

jpegoptim for Improving Page Speed
Optimize and Compress JPEG Images Linux