Three command-line tools dominate JPEG optimisation for the web: jpegoptim, MozJPEG, and jpegtran. Each serves a different purpose in a compression pipeline. Pick the wrong one and you waste time or degrade image fidelity. In a jpegoptim vs mozjpeg comparison, the main distinction is that jpegoptim works on JPEG files you already have, while MozJPEG is an encoder that compresses from source images. jpegtran, which ships with libjpeg and libjpeg-turbo, handles transformations without recompressing pixels.

What Each Tool Does at a Glance

All three tools produce standard JPEG files that any browser can decode, but their inputs and operations differ.

  • jpegtran: Performs transformations of JPEGs without decoding and re-encoding pixels. It can rotate and flip without loss (for example -rotate 90), and it can optimise Huffman tables or rewrite as progressive.
  • MozJPEG: An encoder project launched by Mozilla in 2014. Its cjpeg tool encodes from uncompressed or source images. It produces smaller JPEGs using techniques such as trellis quantisation.
  • jpegoptim: Works on JPEG files you already have. It can do lossless optimisation (rewriting Huffman tables, stripping metadata) or lossy re-encoding with the --max option.

Jpegtran Lossless Operations Only

jpegtran is the tool to reach for when you need to modify a JPEG without recompressing it. Because it never decodes the image data, fidelity stays identical to the original.

Common jpegtran tasks

Use jpegtran -optimize to build optimal Huffman tables, which can reduce file size without any pixel change. Add -progressive to write a progressive JPEG, and -copy none to drop metadata. You can also rotate or flip: jpegtran -rotate 90 input.jpg output.jpg works without loss because jpegtran manipulates the DCT blocks directly.

jpegtran ships with libjpeg and libjpeg-turbo, so it is widely available. It is the right choice for lossless JPEG operations where you want to keep every pixel unchanged.

MozJPEG an Encoder for Source Images

MozJPEG is not a tool for working with JPEGs you already have. Its cjpeg command encodes from uncompressed images (like PNG, TIFF, or PPM) into JPEG. MozJPEG's strength is that it produces smaller files than the baseline libjpeg encoder, thanks to techniques such as trellis quantisation.

When to use MozJPEG

If you have original source images and want to generate the smallest possible JPEGs for web delivery, MozJPEG is a strong choice. It is most effective when compressing from originals, because the encoder can control quantisation and entropy coding from scratch. You cannot run MozJPEG on a JPEG that is already compressed and expect a fidelity improvement without decoding and re-encoding.

For a best jpeg optimizer command line that works on files you already have, MozJPEG is not the answer. It belongs at the start of a pipeline, not in the middle.

Jpegoptim Optimise or Re-Encode Existing JPEGs

jpegoptim is the most flexible tool for working with JPEGs you already have. It can run losslessly (rewriting Huffman tables, removing metadata) or perform lossy re-encoding with the --max option to set a maximum quality.

Lossless optimisation with jpegoptim

By default, jpegoptim strips metadata and optimises Huffman tables, similar to jpegtran -optimize -copy none. Because it operates on the compressed data, the image fidelity is retained exactly.

Lossy re-encoding

When you pass --max with a quality setting, jpegoptim decodes the JPEG, re-encodes it at that quality, and saves the result. This can produce smaller files but sacrifices fidelity. Use this only when you can accept some degradation.

For a best jpeg optimizer command line that handles both lossless and lossy workflows on JPEGs you already have, jpegoptim is the go-to tool.

Lossless JPEG Tools Compared

If your goal is to optimise a JPEG without changing pixels, you have two options: jpegtran and jpegoptim (in lossless mode). Both can strip metadata and rebuild Huffman tables. The main difference is that jpegtran can also rotate and flip losslessly, while jpegoptim offers lossy re-encoding as an option.

For a pure lossless jpeg tools compared scenario, choose jpegtran if you need geometric transforms. Choose jpegoptim if you want a single tool that can do both lossless optimisation and optional lossy compression.

How to Choose for Your Pipeline

Your decision depends on where the JPEG comes from and what you want to do.

  • Source images (PNG, TIFF, etc.): Use MozJPEG's cjpeg for encoding. It will produce the smallest files from originals.
  • Existing JPEGs, no quality change: Use jpegtran (if you also need rotation/flipping) or jpegoptim (if you only need optimisation).
  • Existing JPEGs, willing to lose quality for smaller size: Use jpegoptim with --max.
  • Batch processing in scripts: All three are command-line tools that work well in pipelines. jpegtran and jpegoptim accept standard input/output redirection.

For a best jpeg optimizer command line that covers the most common cases (optimising existing web JPEGs), jpegoptim offers the simplest all-in-one approach.

Practical Steps for a Typical Optimisation

Here is a typical workflow for optimising a folder of JPEGs you already have for the web, using jpegoptim:

  1. Back up your originals.
  2. Run jpegoptim *.jpg for lossless optimisation (strips metadata, optimises Huffman tables).
  3. Check file sizes. If you need smaller files, run jpegoptim --max with a quality setting for lossy re-encoding.
  4. If you need lossless rotation, use jpegtran -rotate 90 -optimize -copy none input.jpg output.jpg instead.
  5. If you are encoding from source images, use cjpeg with a quality setting input.png > output.jpg with MozJPEG.

For more details on jpegoptim commands, visit jpegoptim.com.