MozJPEG is an image compression library build by folks over at mozilla. It is based on libjpeg-turbo.

MozJPEG is used internally by Imagecompressor.io to compress jpeg files.

Installation

Windows

Download Windows Binaries/executables for MozJPEG 3.3.1

32 Bit, mozjpeg-x32.zip
64 Bit, mozjpeg-x64.zip

OSX & Linux.

Install Via Brew.

brew install mozjpeg

OR

Download OSX Binaries for MozJPEG 3.3.1

mozjpeg-3.3.1-osx.zip

Creating Symbolic Links, OSX, Linux/Unix [optional]

If you have libjpeg installed, then cjpeg & jpegtran utilities will be already installed. To avoid any compatibility issues with programs using libjpeg or its utilities we can create symbolic links to cjpeg and jpegtran binaries from mozjpeg.

ln -s /usr/local/Cellar/mozjpeg/3.3.1/bin/cjpeg /usr/local/bin/mozcjpeg

ln -s /usr/local/Cellar/mozjpeg/3.3.1/bin/jpegtran /usr/local/bin/mozjpegtran

In the examples below, we will refer to cjpeg. It should be mozcjpeg or whatever alias you have given while creating symbolic links.

Lossy & Lossless compression

MozJPEG supports both Lossy & Lossless compression. It comes with two executables/binaries, cjpeg & jpegtran.

cjpeg = lossy.
jpegtran = lossless.

tip: run lossless compression after lossy for best results.

cjpeg usage (lossy)

Syntax

cjpeg [switches] image-path > output-file-path

Example

cjpeg /Users/ironman/bird.jpg > /Users/ironman/bird-compressed.jpg 

Most Useful Switches

-quality N, Image Quality, N between 5-95, default 75

This switch lets you trade off compressed file size against quality of the reconstructed image: the higher the quality setting, the larger the JPEG file, and the closer the output image will be to the original input. It is recommended to keep the quality setting between 60-80 for optimum results.

cjpeg -quality 80 /Users/ironman/bird.jpg > /Users/ironman/bird-compressed.jpg

If no quality argument us passed, the encoder uses -quality 75 as default.

-progressive (enabled by default), -baseline (disable progressive)

cjpeg -baseline /Users/ironman/bird.jpg > /Users/ironman/bird-compressed.jpg

-grayscale , Remove colors, Create monochrome JPEG file from color input.

cjpeg -grayscale /Users/ironman/bird.jpg > /Users/ironman/bird-compressed.jpg

jpegtran usage (lossless)

Syntax

jpegtran [switches] image-path > output-file-path

Example & recommended usage -

jpegtran -optimize -progressive image-path > outfile

You can find complete list of switches for jpegtran below.

All cjpeg switches explained

switch description
-quality N Compression quality, 5-95 is most useful range, default is 75)
-quality N[,...] Compression quality (0..100)
-grayscale Create monochrome JPEG file
-rgb Create RGB JPEG file
-optimize Optimize Huffman table (smaller file, but slow compression, enabled by default)
-progressive Create progressive JPEG file (enabled by default)
-baseline Create baseline JPEG file (disable progressive coding)
-targa Input file is Targa format (usually not needed)
-revert Revert to standard defaults (instead of mozjpeg defaults)
-fastcrush Disable progressive scan optimization
-dc-scan-opt - DC scan optimization mode
- 0 One scan for all components
- 1 One scan per component (default)
- 2 Optimize between one scan for all components and one scan for 1st component plus one scan for remaining components
-notrellis Disable trellis optimization
-trellis-dc Enable trellis optimization of DC coefficients (default)
-notrellis-dc Disable trellis optimization of DC coefficients
-tune-psnr Tune trellis optimization for PSNR
-tune-hvs-psnr Tune trellis optimization for PSNR-HVS (default)
-tune-ssim Tune trellis optimization for SSIM
-tune-ms-ssim Tune trellis optimization for MS-SSIM
Switches for advanced users  
-noovershoot Disable black-on-white deringing via overshoot
-arithmetic Use arithmetic coding
-dct int Use integer DCT method (default)
-dct fast Use fast integer DCT (less accurate)
-dct float Use floating-point DCT method
-quant-baseline Use 8-bit quantization table entries for baseline JPEG compatibility
-quant-table N Use predefined quantization table N:
- 0 JPEG Annex K
- 1 Flat
- 2 Custom, tuned for MS-SSIM
- 3 ImageMagick table by N. Robidoux
- 4 Custom, tuned for PSNR-HVS
- 5 Table from paper by Klein, Silverstein and Carney
-restart N Set restart interval in rows, or in blocks with B
-smooth N Smooth dithered input (N=1..100 is strength)
-maxmemory N Maximum memory to use (in kbytes)
-outfile name Specify name for output file
-memdst Compress to memory instead of file (useful for benchmarking)
-verbose or -debug Emit debug output
-version Print version information and exit

All jpegtran switches explained

switch description
-copy none Copy no extra markers from source file
-copy comments Copy only comment markers (default)
-copy all Copy all extra markers
-optimize Optimize Huffman table (smaller file, but slow compression)
-progressive Create progressive JPEG file
Switches for modifying the image  
-crop WxH+X+Y Crop to a rectangular subarea
-flip [horizontal\|vertical] Mirror image (left-right or top-bottom)
-grayscale Reduce to grayscale (omit color data)
-perfect Fail if there is non-transformable edge blocks
-rotate [90\|180\|270] Rotate image (degrees clockwise)
-scale M/N Scale output image by fraction M/N, eg, 1/8
-transpose Transpose image
-transverse Transverse transpose image
-trim Drop non-transformable edge blocks
-wipe WxH+X+Y Wipe (gray out) a rectangular subarea
Switches for advanced users  
-arithmetic Use arithmetic coding
-restart N Set restart interval in rows, or in blocks with B
-maxmemory N Maximum memory to use (in kbytes)
-outfile name Specify name for output file
-verbose or -debug Emit debug output
Switches for wizards  
-scans file Create multi-scan JPEG per script file