Skip to content

Instantly share code, notes, and snippets.

@dkorpar
Created March 19, 2018 21:26
Show Gist options
  • Save dkorpar/8ca6616c939f2db56c15ed520af087c4 to your computer and use it in GitHub Desktop.
Save dkorpar/8ca6616c939f2db56c15ed520af087c4 to your computer and use it in GitHub Desktop.
Optimize/compress JPEG/JFIF files

Optimize/compress JPEG/JFIF files

Jpegoptim is used to optimize/compress jpeg files. Program supports lossless optimization, which is based on optimizing the Huffman tables. And so called "lossy" optimization where in addition to optimizing Huffman tables user can specify upperlimit for image quality.

NOTE! By default jpegoptim modifies the input files (if they are optimized), to preserve original files use option -d to specify alternate directory for saving the optimized files to.

Only normal files are optimized (symbolic links and special files are skipped). Also, any other hard links to the file being optimized are unaf-fected.

Installation

Ubuntu:

$ [sudo] apt-get install jpegoptim

macOS with Homebrew:

$ brew install jpegoptim

Usage

An example of running jpegoptim recursively from the current directory. All images are compressed, with preserved permissions and modification times, progressive and with stripped (most of) metadata.

find . -type f -iname "*.jpg" -exec jpegoptim -p -P -m70 --strip-all --all-progressive {} \;
  • -p Preserve file modification times.
  • -P Preserve file permissions (owner/group) by overwriting the original file.
  • -m Quality. Sets the maximum image quality factor 0 - 100.
  • --all-progressive Force all output files to be progressive.
  • --strip-all, -s Strip all markers from output file.

The example was configured for a personal usage to have smaller file sizes with more or less good quality. Check help $ man jpegoptim for more parameters and their usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment