Skip to main content

compressJPG

compressJPG optimizes.jpg /.jpeg files with a two-step pipeline:

Pathbuild/compressJPG
LanguageBash
Lines83
Sourcebuild/compressJPG
at 406d3e423

Read this when Use this page when tracing shell automation, compression, upload/download helpers, linting, or preprocessing around compressJPG.

Overview

compressJPG optimizes .jpg / .jpeg files with a two-step pipeline:

  1. jpegtran for progressive/optimized re-encoding with metadata stripping
  2. mogrify -quality 60 for additional size reduction

It computes size ratio (original / new) and only replaces the original if ratio is greater than 1.2 (roughly >20% savings).

Input Behavior

  • If file arguments are provided, it processes those files.
  • If no arguments are provided, it auto-discovers JPEG files in the current directory (non-recursive).
  • If neither yields files, it exits with an error.
  • Each file's actual type is checked via file --mime-type: files containing PNG data (mis-labeled files, common in SingleFile-generated web snapshots) are delegated to compressPNG without changing the extension; files that are neither JPEG nor PNG data are skipped with an error message.

Processing Steps

For each file:

  1. Detect MIME type with file; delegate PNG data to compressPNG, skip non-JPEG/non-PNG data
  2. Copy original to a temp file
  3. Run jpegtran -copy none -progressive -optimize
  4. Run mogrify -quality 60 on the temp result
  5. Compare byte sizes with wc -c
  6. Replace original only if compression gain is sufficient

Temporary files are cleaned up at the end of each iteration.

Dependencies

  • jpegtran
  • mogrify
  • bc
  • file (MIME-type detection)
  • compressPNG (sibling script, used when a .jpg file turns out to contain PNG data)
  • standard POSIX tooling (find, wc, mktemp, cp, mv, rm)

Notes

  • Uses set -e for fail-fast behavior.
  • Color helper functions are inlined for portability (instead of sourcing build/bash.sh).
  • Uses a conservative replacement threshold to avoid unnecessary churn on low-gain recompressions.

See Also