Automating image compression locally
I recently poked around some command-line tools for compression. There are a lot of options, but the best seem to be pngquant and jpegoptim. They are fairly cross-platform and free. Both big bonuses in my book.
I want to use CLI tools because they:
- Work across entire projects, not one file at a time
- Are easy to automate (CI, build scripts, pre-deploy steps)
- Produce consistent, repeatable results
- Are not GUI tools which means no manual exporting required

Compressed using pngquant - 59,449 bytes
Step 1: Install the Tools
macOS (Homebrew)
bashbrew install pngquant jpegoptim
Linux
Ubuntu / Debian:
bashsudo apt update
sudo apt install pngquant jpegoptim
Fedora:
bashsudo dnf install pngquant jpegoptim
Arch:
bashsudo pacman -S pngquant jpegoptim
You can then verify installation with:
bashpngquant --version
jpegoptim --version
Step 2: Compress PNGs
Single PNG file compression
bashpngquant --quality=65-85 --force --ext .png image.png
Recursively compress all PNGs in a project
bashfind ./website-folder -type f -name "*.png" -exec pngquant --skip-if-larger --ext .png --quality=65-85 {} \;
This command recursively finds all PNG files, compresses them in place, preserves filenames, and applies a quality range suitable for most web UI and illustration assets. It will skip the file if larger, meaning that it won't try to recompress files that it has already compressed.
Step 3: Compress JPGs
Single JPG file compression
bashjpegoptim --strip-all --max=85 image.jpg
Recursively compress all JPGs in a project
bashfind ./website-folder \( -iname "*.jpg" -o -iname "*.jpeg" \) -type f -exec jpegoptim --strip-all --max=85 {} \;
--max=85 applies strong compression with minimal visual loss, while --strip-all removes EXIF and metadata. Files are overwritten in place.
Optional: Parallel Processing!
Parallel execution is especially useful on Linux servers and modern multi-core Macs.
bash# PNGs
find ./website-folder -type f -name "*.png" -print0 \
| xargs -0 -P 4 pngquant --force --ext .png --quality=65-85
# JPGs
find ./website-folder \( -iname "*.jpg" -o -iname "*.jpeg" \) -type f -print0 \
| xargs -0 -P 4 jpegoptim --strip-all --max=85
Increase the -P value based on available CPU cores. These commands are safe for CI runners and build servers.
Optional: Skip Small Images
You can avoid processing files that won’t meaningfully benefit from compression.
bash# PNGs larger than 50KB
find ./website-folder -type f -name "*.png" -size +50k \
-exec pngquant --force --ext .png --quality=65-85 {} \;
# JPGs larger than 50KB
find ./website-folder \( -iname "*.jpg" -o -iname "*.jpeg" \) -type f -size +50k \
-exec jpegoptim --strip-all --max=85 {} \;