Linux Jpeg Image Optimization Command

Days ago we wrote a very useful tutorial on how to optimize / compress PNG files on Linux using the fantastic optipng command. However, lot of users have JPG / JPEG files also, which are widely used for high resolution photographies. What’s the Linux jpeg image optimization command to compress those files?

What is JPG format?

JPG file format is one of the oldest and most used image formats on the world wide web. It’s really useful and suggested for high resolution photo images. JPEG allows several degrees of compression that can be manually adjusted when you take a picture, or edit a picture on a photo editor, the compression level can be set betwen 10:1 with little to none perceptible loss in photography quality.

Jpegoptim: the best Linux Jpeg image optimization command

jpeg photos can eat your bandwidth if you don’t optimize them. Lot of users usually take a picture with their phones or digital cameras, and due to the high resolution / quality of those cameras, the result is a great picture, however when you upload those images into a website, you will notice they take forever to load.

On this post we will try to answer questions like:

  • How can I compress / optimize JPEG images on my Linux server?
  • How can I save bandwidth on my CDN images on Amazon Cloud front, Cloudflare or MaxCDN?
  • Is it possible to use an image compressor to create lossless compression on JPEG / JPEG filesin bulk using Linux, and at the same time avoid image quality loss?

Let’s see what are the best options to compress jpeg files, there are lot of jpeg compression tools online, however, if you have full root control over your Linux / Unix server, there is a better way to optimize jpeg, we will use a famous jpeg compression tool (a jpg shrinker) that will do all the job for you.

Forget about slow old ways to optimize jpeg and optimize jpg files using online compressors, with jpegtrim you will be able to even compress multiple jpg files with a single command. Let’s begin.

Optimize your JPEG files in two ways

1) Reduce photo resolution

You can use simple image editors like GIMP, that is available for all major linux distributions. If you take a picture and it is 2000×1024 pixels, reduce it to the half, you will still have a nice big image, and your jpeg size will be 50% less.

2) Use jpegoptim to optimize JPG size

jpegoptim command is the one that will do all the magic. It is a command line based tool that will help you to optimize/compress jpeg files. This application allows you lossless optimization by optimizingthe JPG Huffman tables.  This is also known as “lossy” image optimization.

This command is useful for any kind of Linux based server, it will work on dedicated servers, vps servers, cloud servers, and of course, if you use a CDN like Akamaik, Cloudfront, Cloudflare, StackPath (MaxCDN), it will help you to reduce the final size of the file, saving bandwidth on your CDN provider day by day.

Install jpegoptim on Ubuntu/Debian

Run this apt-get command to install this tool on Debian based servers:

apt-get install jpegoptim

Install jpegoptim on CentOS/RHEL/Fedora

Install EPEL repo to ensure you will get jpegoptim installed:

yum install epel-release

Install the package:

yum install jpegoptim


Sample outputs:

jpeoptim, the best Linux Jpeg image optimization command
Fig. 01. Screenshot of jpeoptim installation on Linux

jpegoptim syntax / usage

The command usage is pretty simple:

  • jpegoptim
  • jpegoptim [options]

In order to optimize a photo, you can try as you see above:

jpegoptim your-photo.jpeg

Sample outputs for photo files:

[[email protected]:~]jpegoptim Fig.-01.-Screenshot-of-jpeoptim-installation-on-Linux-624x599.jpg
Fig.-01.-Screenshot-of-jpeoptim-installation-on-Linux-624x599.jpg 624x599 24bit N JFIF [OK] 53186 --> 51301 bytes (3.54%), optimized.
[[email protected]:~]

How do I process this jpeg shrinker in batch / bulk?

You can use a classic for loop from bash, for example:

for i in 1.jpeg 2.jpeg 3.jpeg; do jpegoptim "$i"; done

jpegoptim Options

This are the available options from the man page by running jpegoptim –help:

jpegoptim v1.4.4  Copyright (c) 1996-2016, Timo Kokkonen
Usage: jpegoptim [options]  

  -d, --dest=
                    specify alternative destination directory for 
                    optimized files (default is to overwrite originals)
  -f, --force       force optimization
  -h, --help        display this help and exit
  -m, --max=
                    set maximum image quality factor (disables lossless
                    optimization mode, which is by default on)
                    Valid quality values: 0 - 100
  -n, --noaction    don't really optimize files, just print results
  -S, --size=
                    Try to optimize file to given size (disables lossless
                    optimization mode). Target size is specified either in
                    kilo bytes (1 - n) or as percentage (1% - 99%)
  -T, --threshold=
                    keep old file if the gain is below a threshold (%)
  -b, --csv         print progress info in CSV format
  -o, --overwrite   overwrite target file even if it exists (meaningful
                    only when used with -d, --dest option)
  -p, --preserve    preserve file timestamps
  -P, --preserve-perms
                    preserve original file permissions by overwriting it
  -q, --quiet       quiet mode
  -t, --totals      print totals after processing all files
  -v, --verbose     enable verbose mode (positively chatty)
  -V, --version     print program version

  -s, --strip-all   strip all markers from output file
  --strip-none      do not strip any markers
  --strip-com       strip Comment markers from output file
  --strip-exif      strip Exif markers from output file
  --strip-iptc      strip IPTC/Photoshop (APP13) markers from output file
  --strip-icc       strip ICC profile markers from output file
  --strip-xmp       strip XMP markers markers from output file

  --all-normal      force all output files to be non-progressive
  --all-progressive force all output files to be progressive
  --stdout          send output to standard output (instead of a file)
  --stdin           read input from standard input (instead of a file)

At this time you should be able to use jpegoptim, probably the best Linux Jpeg Image Optimization Command available for modern Linux distros.

Do you know other commands to optimize JPEG/JPG files on Linux / Unix? Share your knowledge with us.

Further reading:

About the Author: Santiago Borges

Experienced Sr. Linux SysAdmin and Web Technologist, passionate about building tools, automating processes, fixing server issues, troubleshooting, securing and optimizing high traffic websites.

Leave a Reply

Your email address will not be published. Required fields are marked *