🔧

Image Optimization

1 notes  •  Tools & Utilities

Losslessly Compress JPEG Images in Linux with jpegoptim

jpegoptim reduces JPEG file sizes without visible quality loss by stripping metadata and optimizing Huffman coding. It can process entire directories in batch.

Prerequisites

  • Ubuntu/Debian server with internet access

Install jpegoptim

# From the repository
apt-get install jpegoptim

# Or compile from source for the latest version
apt-get install build-essential libjpeg-dev
wget https://github.com/tjko/jpegoptim/archive/refs/heads/master.tar.gz
tar xzf master.tar.gz && cd jpegoptim-master
./configure && make && make install

Compress Images

# Compress a single image (lossless, in-place)
jpegoptim image.jpg

# Compress to a target quality (e.g. 85%)
jpegoptim --max=85 image.jpg

# Batch compress all JPEGs in the current directory
find . -name '*.jpg' -exec jpegoptim {} \;

# Batch compress and strip all metadata (EXIF, IPTC)
find . -name '*.jpg' -exec jpegoptim --strip-all {} \;

# Save to a different directory instead of in-place
jpegoptim --dest=/path/to/output/ *.jpg

Verify

ls -lh before.jpg after.jpg

Compare file sizes to confirm reduction. Visual quality should be unchanged for lossless compression.

Notes

  • Lossless compression typically achieves 5–20% reduction; lossy (--max) can achieve more.
  • Use optipng or pngquant for PNG images.
  • For a WordPress alternative, use the EWWW Image Optimizer plugin which calls jpegoptim under the hood.