cookie 100k
A small cat, a large file-size

Let’s say you need to compress a JPEG image on Linux — how do you do it?

Well, you might reach for a GUI app, like GIMP, and play around with settings.

But that requires effort.

So in this post we show you a command line tool that lets you quickly compress jpeg images on Linux, without any fuss.

If you don’t like the command line try the small image optimization app ‘Trimage’, which provides a GUI front-end for jpegoptim (and pngcrush).

How to Install jpegoptim on Ubuntu

First things first: let’s get the tool set-up on your Linux machine.

As jpegoptim is a command line app we’ll show you how to use the command line to install it. This is entirely optional; you can also install the app using the Ubuntu Software Center (or a similar package management tool).

Open a new Terminal session and enter the following command, followed by your password as prompted:

sudo apt-get install jpegoptim

The app is tiny so won’t take long to install. Once it has it’s ready to use.

Using Jpegoptim

jpegoptim command

Jpegoptim is a small utility that can compress and optimize JPEG files without significant lose in quality — but a notable difference in file size.

Compression can help save on bandwidth (if using images on a website) and make sending emails a bit faster (if you have a slow connection).

Using the tool is simple.

The default command optimizes your jpg images using maximum/lossless settings to offer the best image quality. To process an image you run the ‘jpegoptim’ command followed by the location of the image you wish to compress.

For example, I have a 1.2MB photo of Cookie in my Home folder. To compress it using Jpegoptim I would open a new Terminal session and run:

jpegoptim cookie.jpeg

After the command has ‘done its thang’ — and bear in mind it will overwrite the original file by default — I can check the result. And, sure enough, the file is reduced from 1.2MB to …1.1MB.

Small change, right?

But that’s where we can get fancy and Jpegoptim can show off. The app also lets you reduce a jpg to a specific size. 

An (obvious) tradeoff in setting an arbitrary file size limit is image quality. By Ssecifying a file size you disable the lossless optimization and the image quality takes a nosedive as a result.

But it’s using this option that we can dramatically reduce jpeg size.

terminal command

To go back to our example, let’s re-compress our photo of Cookie to something more web-friendly and take it from 1.1MB to a more manageable 100k.

We can specify a file size in either kilobytes (k) or as a percentage (%) and, just to confuse things we’ll need to tell the app that it can overwrite the previous attempt to compress it.

jpegoptim --size=100k cookie.jpeg --overwrite

Note that the argument stating the size comes before the image location but that the okay to overwrite comes after it.

The resulting image is roughly my chosen file-size (a rogue kb over) and has dropped in quality as a result:

cookie 100k

Now let’s be crazy and reduce the image to just 15kb! This time we’ll also choose to save the file in a different location using the ‘–dest‘ flag:

jpegoptim --size=15k cookie.jpg --dest ~/Desktop

The result:


Other Features

It is also possible to optimise several images at once simply by listing them:

jpegoptim kittens.jpeg cats.jpg feline_good.jpg

Need to batch process an entire folder of jpegs? Jpegoptim can handle that too:

jpegoptim ~/Pictures/CatPhotos/*.jpeg

To see a full list of options and usage instructions for this utility see the Ubuntu manpage by running:

man jpegoptim

Do you have a favourite command-line utility? Let us know about it in the comments below or by sending us a tip!

commandline jpegoptim