compress

Compress images with advanced quality control. Reduce file sizes significantly while maintaining visual quality through fine-grained compression settings.

Usage

Terminal
$ mediaproc image compress <input> [options]

Description

The compress command provides aggressive image compression with fine-grained quality control. Unlike optimize which focuses on balanced size/quality trade-offs, compress allows you to push compression further for maximum file size reduction. Perfect for thumbnails, backgrounds, or situations where smaller file sizes are prioritized.

Quality vs File Size Trade-off

Lower quality settings (below 60) can significantly reduce file size but may introduce visible artifacts. Always preview compressed images before using them in production. Use the --dry-run flag to see potential savings without modifying files.

Options

OptionTypeDefaultDescription
<input>stringrequiredInput image file path
-o, --outputpath<input>-compressed.<ext>Output file path
-q, --qualitynumber75Compression quality (1-100)
--lossyflag-Enable lossy compression for maximum reduction
--dry-runflag-Preview changes without executing
-v, --verboseflag-Show detailed output

Examples

Basic Compression

Terminal
$ mediaproc image compress photo.jpg
✓ Compressed: 2.45 MB → 1.12 MB (54% reduction)
Terminal
$ mediaproc image compress image.png -q 60
✓ Compressed: 845 KB → 312 KB (63% reduction)
Terminal
$ mediaproc image compress pic.jpg --lossy
✓ Compressed: 3.2 MB → 890 KB (72% reduction)
Terminal
$ mediaproc image compress photo.webp -q 80 -o small.webp
✓ Compressed: 1.8 MB → 945 KB (48% reduction)

Batch Compression

Terminal
$ for img in *.jpg; do mediaproc image compress "$img" -q 70; done
✓ Processed 15 images
✓ Total reduction: 18.5 MB → 6.2 MB (66%)
Terminal
$ mediaproc image compress large-image.jpg --lossy -o thumbnail.jpg
✓ Compressed: 5.8 MB → 425 KB (93% reduction)

Format-Specific Examples

JPEG Compression
$ mediaproc image compress photo.jpg -q 75
✓ Progressive encoding enabled
✓ Compressed: 2.4 MB → 1.1 MB (54%)
PNG Compression
$ mediaproc image compress screenshot.png --lossy
✓ Palette reduced to 256 colors
✓ Compressed: 892 KB → 234 KB (74%)
WebP Compression
$ mediaproc image compress image.webp -q 60
✓ Compressed: 1.5 MB → 580 KB (61%)
AVIF Compression
$ mediaproc image compress photo.avif -q 70
✓ Compressed: 2.2 MB → 512 KB (77%)

Quality Levels

  • Quality 80-100: High quality, moderate compression

    • Best for: Hero images, portfolios, print-ready
    • Typical reduction: 10-30%
  • Quality 60-79: Good quality, good compression (recommended)

    • Best for: General web use, galleries, blogs
    • Typical reduction: 30-60%
  • Quality 40-59: Lower quality, high compression

    • Best for: Backgrounds, decorative images
    • Typical reduction: 60-80%
  • Quality 1-39: Low quality, maximum compression

    • Best for: Placeholders, low-priority assets
    • Typical reduction: 80-90%

Lossy Mode

Enabling --lossy activates aggressive compression algorithms:

  • JPEG: Uses MozJPEG for better compression
  • PNG: Converts to indexed palette (256 colors)
  • WebP: Switches to lossy encoding
  • Results in visible artifacts but smallest files

Format-Specific Behavior

JPEG Compression

  • Progressive encoding for faster loading
  • MozJPEG optimization (with --lossy)
  • Metadata stripping for size reduction

PNG Compression

  • Maximum compression level (9)
  • Optional palette reduction (with --lossy)
  • Alpha channel preservation

WebP Compression

  • Lossless by default
  • Lossy mode for better compression
  • Superior to JPEG at same quality

AVIF Compression

  • Next-generation format
  • 50% smaller than JPEG
  • Slower encoding, best quality

Use Cases

Web Optimization

Terminal
$ mediaproc image compress hero.jpg -q 75 -o hero-web.jpg
✓ Compressed: 3.2 MB → 1.4 MB (56% reduction)
Terminal
$ mediaproc image compress large.jpg -q 50 --lossy -o thumb.jpg
✓ Compressed: 4.5 MB → 680 KB (85% reduction)

Social Media

Terminal
$ mediaproc image compress photo.jpg -q 70 -o instagram.jpg
✓ Compressed: 2.8 MB → 1.1 MB (61% reduction)
Terminal
$ mediaproc image compress pic.jpg -q 65 -o twitter.jpg
✓ Compressed: 3.5 MB → 890 KB (75% reduction)

Email Attachments

Terminal
$ mediaproc image compress photo.jpg -q 60 --lossy
✓ Compressed: 2.5 MB → 420 KB (83% reduction)

Storage Optimization

Terminal
$ for img in archive/*.jpg; do mediaproc image compress "$img" -q 65; done
✓ Processed 247 images
✓ Total space saved: 1.2 GB

vs Optimize

When to use compress:

  • Need aggressive size reduction
  • Quality is secondary to file size
  • Creating thumbnails or previews
  • Storage/bandwidth constrained
  • Can tolerate some artifacts

When to use optimize:

  • Need best quality/size balance
  • Want minimal quality loss
  • Processing customer photos
  • Professional photography
  • SEO and UX matter

Technical Details

Compression Algorithms

JPEG:

  • Baseline: Standard JPEG compression
  • Progressive: Loads incrementally (better UX)
  • MozJPEG: Advanced optimization (--lossy)

PNG:

  • zlib compression (level 9)
  • Palette optimization
  • Color reduction (--lossy)

WebP:

  • Lossless: Predictive coding
  • Lossy: Transform coding like JPEG
  • Better compression than JPEG/PNG

AVIF:

  • AV1 image format
  • Superior compression ratio
  • Slower encoding time

Output Information

The command displays:

  • Original file size
  • Compressed file size
  • Reduction percentage
  • Processing time (verbose mode)
  • Quality settings used

Tips

  • Quality 60-75 offers the best compression/quality trade-off for most use cases
  • Use --lossy for thumbnails and backgrounds where quality matters less
  • WebP format achieves better compression than JPEG at equivalent quality
  • Test different quality levels to find the sweet spot for your specific images
  • Always keep originals before compressing, especially with lossy mode
  • Combine with resize for even greater size reduction
  • Use verbose mode to see detailed compression statistics

Troubleshooting

File size not reducing much

  • Try lower quality values (-q 50-60)
  • Enable lossy mode (--lossy)
  • Consider format conversion to WebP/AVIF
  • Check if image is already optimized

Quality too low

  • Increase quality value (-q 80-90)
  • Disable lossy mode
  • Try optimize command instead
  • Use original higher-resolution source

Slow processing

  • AVIF format is slower to encode
  • Use JPEG/WebP for faster compression
  • Process smaller images first
  • Consider batch processing overnight

See Also

  • optimize - Balanced size optimization
  • convert - Format conversion
  • resize - Resize before compressing
  • batch - Batch process multiple images

Found an issue? Help us improve this page.

Edit on GitHub →