compress
Compress images with advanced quality control. Reduce file sizes significantly while maintaining visual quality through fine-grained compression settings.
Usage
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.
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
| Option | Type | Default | Description |
|---|---|---|---|
<input> | string | required | Input image file path |
-o, --output | path | <input>-compressed.<ext> | Output file path |
-q, --quality | number | 75 | Compression quality (1-100) |
--lossy | flag | - | Enable lossy compression for maximum reduction |
--dry-run | flag | - | Preview changes without executing |
-v, --verbose | flag | - | Show detailed output |
Examples
Basic Compression
Batch Compression
Format-Specific Examples
Quality Levels
Recommended Settings
-
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
Social Media
Email Attachments
Storage Optimization
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