Let’s say you need to compress and optimize a JPEG image on Linux.
How do you do it?
You might reach for a GUI app, like GIMP, and play around with settings.
But that requires effort.
In this post we’re going to look at using a command line tool to effortlessly compress images on Linux.
How to Install jpegoptim on Ubuntu
First things first: let’s get the tool installed 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 on Ubuntu
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 couldn’t be simpler.
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:
After the command has ‘done its thang’ 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.
There is an (obvious) tradeoff in setting an arbitrary file size limit: image quality. Specifying a file size will disable the lossless optimization (i.e., your image quality will take a nosedive) but it can dramatically reduce jpeg size.
To go back to our example, let us re-compress our photo of Cookie to something more web-friendly. Let’s reduce it from 1.1MB to a more manageable 100k (you can specify a file size in either kilobytes (k) or as a percentage (%):
jpegoptim --size=100k cookie.jpeg
Note that the argument stating the size comes before the image location and not after it.
The resulting image is roughly my chosen file-size (a rogue kb over) but has dropped in quality.
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:
To see a full list of options and usage instructions for this utility run the following command:
Do you have a favourite command-line utility? Let us know about it in the comments below or by sending us a tip.
This article is part of our Linux Photography Toolbox series. Click here to see other entries.