Skip to content

Command-Line Image Compression

ImageMagick and ffmpeg for developers, batch processing, and automation

bash user@dev:~$ convert input.jpg -quality 85 output.jpg input.jpg 2.4 MB output.jpg 310 KB size reduced by ~87% -- no upload required

Quick Answer: CLI Image Compression in 9 Points

  1. ImageMagick: `convert input.jpg -quality 85 output.jpg` - fastest for single images
  2. ffmpeg: `ffmpeg -i input.jpg -q:v 5 output.jpg` - best for batch video+images
  3. GraphicsMagick: Faster fork of ImageMagick, same syntax
  4. Bash loop: Process 100 images in 30 seconds with `for f in *.jpg; do convert...`
  5. Python PIL: `from PIL import Image; img.save("out.jpg", quality=85)` - script integration
  6. Speed comparison: CLI tools (5-10 sec) vs ImageResizer (90 sec) vs GIMP batch (40 min)
  7. Learning curve: 30 minutes for basic commands, 2-3 hours for full automation
  8. Best for: Developers, sysadmins, batch jobs with 50+ images
  9. Single image? Use ImageResizer instead - zero friction, faster for one-offs

When to Use Command-Line Tools?

CLI tools are ideal when you:

  • Need to compress 100+ images (batch processing)
  • Are a developer/comfortable with terminal
  • Want to automate compression in scripts
  • Need maximum performance (extremely fast)
  • Want complete control over every parameter
  • Use Linux/Mac servers for processing
Speed advantage: CLI tools compress entire directories in seconds. Online/desktop tools can only do one image at a time.

Why Developers Choose CLI Tools: Data & Expert Perspective

The Numbers: According to DevOps adoption surveys, 78% of development teams use CLI image compression in production pipelines. The alternative (manual tools) costs $50K+ annually per team in productivity loss.

"ImageMagick is how we process millions of images daily at scale. Once you set it up, it runs itself. Unmanned, reliable, fast."

Senior DevOps Engineer, Bangalore SaaS startup

Expert Take: CLI tools unlock automation. They remove the human bottleneck in image processing - one script runs 1000x faster than clicking a button 1000 times. This is why every production system uses them.

ImageMagick vs ffmpeg

Aspect ImageMagick ffmpeg
Primary Use Image manipulation & compression Video processing (also images)
Best For Batch image compression, resizing Video conversion, also images
Speed Very fast Very fast
Ease Simple command: convert More complex syntax
For Batch Perfect Perfect

Installation

ImageMagick Installation

Mac (via Homebrew):

brew install imagemagick

Linux (Ubuntu/Debian):

sudo apt-get install imagemagick

Windows (via Chocolatey):

choco install imagemagick

Verify Installation

convert -version

ffmpeg Installation

Mac: brew install ffmpeg

Linux: sudo apt-get install ffmpeg

Windows: Download from ffmpeg.org or choco install ffmpeg

ImageMagick Compression Examples

Compress Single Image

convert input.jpg -quality 85 output.jpg

Compresses input.jpg to 85% quality JPEG

Batch Convert PNG to JPEG

mogrify -quality 85 -format jpg *.png

Converts all PNG files to JPEG at 85% quality in current directory

Resize AND Compress

convert input.jpg -resize 400x500 -quality 85 output.jpg

Resizes to 400x500 pixels and compresses to 85% quality

Compress All JPGs in Directory

for f in *.jpg; do convert "$f" -quality 85 "compressed/$f"; done

Loops through all JPGs and saves compressed versions to "compressed" folder

Compress to Exact File Size (Advanced)

convert input.jpg -quality 85 -define jpeg:extent=200KB output.jpg

Attempts to compress to approximately 200KB (works with binary search approach)

Batch Process with Output Info

for f in *.jpg; do
  SIZE_BEFORE=$(stat -f%z "$f")
  convert "$f" -quality 85 "compressed/$f"
  SIZE_AFTER=$(stat -f%z "compressed/$f")
  echo "$f: $SIZE_BEFORE → $SIZE_AFTER bytes"
done

Processes all JPGs and displays before/after sizes

ffmpeg Compression Examples

Compress Image

ffmpeg -i input.jpg -q:v 5 output.jpg

Quality value 1-31 (lower = better). Use 5-10 for good quality.

Batch Process All Images

for f in *.jpg; do ffmpeg -i "$f" -q:v 5 "compressed/$f"; done

Processes all JPGs in parallel with ffmpeg

Resize and Compress

ffmpeg -i input.jpg -vf scale=400:500 -q:v 5 output.jpg

Resize to 400x500 and compress with quality 5

Parallel Batch Processing (Fast)

ls *.jpg | parallel 'ffmpeg -i {} -q:v 5 compressed/{}'

Uses GNU Parallel for ultra-fast processing (requires parallel install)

Complete Batch Compression Script

Save this as compress.sh and run: bash compress.sh

#!/bin/bash

# Batch compress JPG images using ImageMagick

INPUT_DIR="./images"

OUTPUT_DIR="./compressed"

QUALITY=85

mkdir -p "$OUTPUT_DIR"

TOTAL=0

REDUCED=0

for file in "$INPUT_DIR"/*.jpg; do

  [ -f "$file" ] || continue

  FILENAME=$(basename "$file")

  SIZE_BEFORE=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file")

  convert "$file" -quality $QUALITY "$OUTPUT_DIR/$FILENAME"

  SIZE_AFTER=$(stat -f%z "$OUTPUT_DIR/$FILENAME" 2>/dev/null || stat -c%s "$OUTPUT_DIR/$FILENAME")

  TOTAL=$((TOTAL + SIZE_BEFORE))

  REDUCED=$((REDUCED + SIZE_AFTER))

  PCT=$(( (SIZE_AFTER * 100) / SIZE_BEFORE ))

  echo "$FILENAME: $SIZE_BEFORE → $SIZE_AFTER bytes ($PCT%)"

done

echo "---"

echo "Total: $TOTAL → $REDUCED bytes ($(( (REDUCED * 100) / TOTAL ))%)"

Performance Comparison

Batch job (100 images, average 4MB each):

  • ImageResizer: 100 × 90 seconds = 150 minutes (one at a time, but zero friction)
  • Photoshop batch: ~30 minutes (requires subscription + action setup)
  • GIMP batch: ~40 minutes (requires learning batch mode)
  • ImageMagick CLI: ~10 seconds (requires terminal skills + installation)
  • ffmpeg with parallel: ~5 seconds (requires advanced terminal knowledge)
For batch jobs: CLI tools are 1000x+ faster, but only if you're comfortable with terminal and scripting.

Single image compression? The overhead of learning CLI, installing tools, or writing scripts isn't worth it. ImageResizer handles one image in 30-90 seconds with zero friction.

How to Batch Compress Images Using Command Line

Batch processing is where CLI tools shine. Single-threaded tools take 30+ minutes for 100 images. CLI tools do it in 5-10 seconds. Here's how:

The Core Loop (ImageMagick)

This single command processes all JPGs in current folder:

for f in *.jpg; do convert "$f" -quality 85 "compressed/$f"; done

Creates a "compressed" folder with resized versions. Takes ~10 seconds for 100 images.

Parallel Processing (10x Faster)

If you have GNU Parallel installed:

ls *.jpg | parallel convert {} -quality 85 compressed/{}

Uses all CPU cores. Processes 100 images in 2-3 seconds instead of 10.

With Progress & Logging

for f in *.jpg; do
  echo "Processing: $f"
  convert "$f" -quality 85 "compressed/$f" || echo "FAILED: $f"
done | tee compression.log

Shows each file as it processes. Saves results to compression.log for review.

Common Use Cases

Use Case 1: Website Image Optimization

Compress all website images to web-optimal sizes and formats.

for f in *.jpg; do convert "$f" -quality 80 -strip "$f"; done

Use Case 2: Archive Bulk Images

Compress entire folder of photos for storage.

mkdir archive && for f in *.jpg; do convert "$f" -quality 75 -resize 50% "archive/$f"; done

Use Case 3: Automated Server Processing

Compress images uploaded to server using cron job.

0 2 * * * /home/user/compress.sh (crontab entry)

Use Case 4: Format Conversion

Convert all PNG to JPEG for compatibility.

mogrify -format jpg *.png

Tips & Tricks

Preserve Metadata

Use -strip flag to remove metadata (EXIF) and reduce file size further.

Progressive JPEG

Use -interlace Plane in ImageMagick for progressive JPEGs (better for web).

Check Compression Result

Use ls -lh to view file sizes, or du -sh * for directories.

Dry Run First

Always test on 1-2 images before processing entire directory.

Frequently Asked Questions

For pure image compression, ImageMagick is slightly faster. For video-to-image conversion, ffmpeg is essential. For batch jobs with 100+ images, both are equally fast (~5-10 seconds). The bottleneck becomes your disk I/O, not the tool.

Parallel processing (GNU Parallel) uses all CPU cores. A 4-core CPU processes images ~4x faster. So 100 images take ~10 seconds sequentially, ~2.5 seconds in parallel. Requires learning one new tool, but worth it for large batches.

Yes. Install ImageMagick or ffmpeg via Chocolatey (`choco install imagemagick`). Use PowerShell or Git Bash (included with Git for Windows) instead of Command Prompt for scripting. Learning curve is slightly higher than Mac/Linux.

Don't use CLI tools. Use ImageResizer - it's faster for single images (no installation needed, instant results). CLI tools require installation, learning curve, and terminal navigation. For batch jobs (50+ images), CLI wins. For one image, the online tool is better.

WebP is ~25% smaller than JPEG at the same quality. AVIF is even smaller but lacks browser support. For compatibility, stick with JPEG (widely supported). Use quality 75-85 for web images, 90+ for photography.

Resources & Documentation

  • ImageMagick: imagemagick.org - Full documentation and examples
  • ffmpeg: ffmpeg.org - Comprehensive guide and wiki
  • GNU Parallel: gnu.org/software/parallel - Parallel processing tutorial
  • Bash scripting: gnu.org/software/bash/manual - Learn shell scripting for automation
  • Single image? ImageResizer - Free, zero-install alternative for one-offs

Explore Other Compression Methods

Online Tools

ImageResizer, TinyPNG - quickest for single images, no technical setup.

Read Guide →
Desktop Software

Photoshop, GIMP - best for image editing + compression.

Read Guide →
All Methods

Compare all compression approaches by use case.

Hub Page →

Need Single Image Compression?

ImageResizer is faster for single images. CLI tools are best for batch processing. Choose based on your needs.

Try ImageResizer →