Optimizing JPEG images with MozJPEG

MozJPEG is a project from Mozilla aimed to improve JPEG compression while keeping full backwards compatibility with current decoders. Recently they released version 3 of the library. Previous version reduced files by 5% on average compared to jpeg-turbo, the JPG compression library it is based on. The new version contains more improvements, such as reducing compression artifacts for text on white background, and better quality for high-resolution images.

Installing and using MozJPEG in OS X

MozJPEG can be installed on a Mac via Homebrew package manager by typing:

brew install mozjpeg

Since mozjpeg is a replacement for libjpeg, to avoid any compatibility issues with programs using libjpeg or its utilities, Homebrew won’t create symlinks to replace libjpeg with it. For convenience, I created the following links to cjpeg and jpegtran binaries from mozjpeg:

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

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

(Note: replace 3.0 with whatever version you have installed, e.g. 3.1)

This way calling cjpeg will execute the libjpeg’s utility, while calling mozcjpeg will use the Mozilla’s version. All following examples will use binaries linked like this.

Converting PNG to JPEG with MozJPEG

To convert PNG to JPG, we’ll first use convert utility from ImageMagick (you probably have it installed; if not, run brew install imagemagick) to convert PNG to a format accepted by cjpeg, and then pipe the result of conversion into MozJPEG’s version of cjpeg to get the JPEG file:

convert filename.png pnm:- | mozcjpeg -quality 90 > filename.jpg

Tweak quality parameter (recommended range is 50 to 90) until you get the results you need.

Optimizing and re-compressing JPEGs

If you want to make your JPEGs smaller with MozJPEG optimizer, you can use jpegtran utility from its distribution:

mozjpegtran -outfile output.jpg -optimise -copy none input.jpg

(again, we’re using mozjpegtran which is symlinked to jpegtran binary from mozjpeg, see above.)

JPEGtran uses lossless optimizations, so there will be no change in quality. If you’d like to re-compress JPEGs, losing some quality, but making it even smaller, you can again use convert utility with cjpeg:

convert filename.jpg pnm:- | mozcjpeg -quality 70 > filename-optimized.jpg

Again, tweak quality option for your taste.


Project information

Authors: Mozilla and libjpeg-turbo contributors
License: 3-clause BSD


  1. I’m on Yosemite. I successfully installed imagemagic and mozjpeg (into Cellar) and made the sym links in /use/local/bin as you suggested, but when I run the convert, bash throws an error saying mozcjpeg cannot be found. But it is finding the convert program in /usr/local/bin and the symlink is there on the same level as convert. Any idea what could be wrong?

  2. Kailasa: try running hash -r in bash. Also, make sure that /usr/local/bin is in your PATH. Alternatively, try giving the full path to the symlink.

  3. @Carter

    duh (foot in mouth) HomeBrew downloaded mozjpeg 3.1

    so, silly me…. these syms links did not work (of course not)

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

    I just had to delete those and redo them as:

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

    Now it’s working… I can build a GUI on Livecode that will drive these for me and batch process etc. Interesting result though: the recommendation on line was to do a lossy compression and then run imageOptim on the same files… to get the smallest file size… but if I use this cmd line API, the resulting file seems to be already as compressed as it can be and imageOptim tell me 0% savings… I saved a 3 MB camera file 2048w X 1920 high at 300 dpi down to 72 DPI (resample on) then ran it through this mozjpeg process and got a 99k File! I nearly fell out of my chair… that’s awesome savings and looks great! Ready to rock and roll on prepping images for a mobile app…

    Now… if I could only get a tool that would take down 300dpi camera images to 72 via cmd line *before* I run them mozjpeg… that would save me a round trip through Photoshop first. I think imageMagic can probably do that if I figure out the commands…

    1. Ha, of course! I’ve update the post to point out version number changes, thanks.

  4. Odd… I am getting a new error… I tried again hard coding the path:

    /usr/local/Cellar/imagemagick/6.9.1-4/bin/convert “/Users/myUser/Documents/Back to Siva App/BTS-Build-Resources/BTS Graphics Working/ardhanarishvara/a_fine_portrait_of_ardhanarishvara_shiva_shakti_tp131-2.jpg” pnm:- | /usr/local/Cellar/mozjpeg/3.1/bin/jpegtran -quality 50 > “/Users/myUser/Documents/Back-to-Siva-App/BTS-Build-Resources/BTS-Graphics-Working/ardhanarishvara/a_fine_portrait_of_ardhanarishvara_shiva_shakti_tp131-2_opt-50.jpg”

    And I get this error: …

    -bash: /Users/myUser/Documents/Back-to-Siva-App/BTS-Build-Resources/BTS-Graphics-Working/ardhanarishvara/a_fine_portrait_of_ardhanarishvara_shiva_shakti_tp131-2_opt-50.jpg: No such file or directory

Comments are closed.