Image Optimizer
Optimize images via compression, resizing, and format conversion.
Use when compressing images, batch converting formats, or resizing for web/social media.
Requirements
- ImageMagick 7+ (
magick) or ImageMagick 6 (convert) - Optional:
cwebp(WebP),optipng(PNG),jpegoptim(JPEG) - No API keys needed
Instructions
Check image info before processing:
magick identify -format "%f | %wx%h | %b | %m\n" image.pngCompress — reduce file size:
# JPEG (80% quality is a good default) magick input.jpg -quality 80 -strip output.jpg # PNG (lossless optimization) optipng -o5 image.png # in-place magick input.png -strip output.png # WebP (lossy, excellent compression) cwebp -q 80 input.png -o output.webpResize — scale to specific dimensions:
# Exact dimensions (may distort) magick input.jpg -resize 800x600! output.jpg # Fit within bounds (preserve aspect ratio) magick input.jpg -resize 800x600 output.jpg # By percentage magick input.jpg -resize 50% output.jpg # Thumbnail (crop to fill) magick input.jpg -resize 300x300^ -gravity center -extent 300x300 thumb.jpgFormat conversion:
magick input.png output.webp # PNG → WebP magick input.jpg output.avif # JPEG → AVIFBatch processing:
# Convert all PNGs to WebP in a directory for f in *.png; do magick "$f" -quality 80 "${f%.png}.webp"; done # Resize all images to max 1200px width for f in *.jpg; do magick "$f" -resize "1200>" "$f"; doneOutput format — report results:
## 🖼️ Optimization Results | File | Original | Optimized | Savings | |------|----------|-----------|---------| | hero.jpg | 2.4 MB | 680 KB | 72% | | logo.png | 340 KB | 89 KB | 74% | **Total: 2.74 MB → 769 KB (72% reduction)**
Preset Sizes
| Use Case | Dimensions | Notes |
|---|---|---|
| Web hero | 1920×1080 | JPEG q80 or WebP |
| Thumbnail | 300×300 | Crop to fill |
| Social (OG) | 1200×630 | Facebook/Twitter preview |
| Profile pic | 400×400 | Square crop |
| 600px wide | Resize width only |
Edge Cases
- Animated GIF: Use
magick input.gif -coalesce -resize 50% output.gif(preserve animation). - EXIF data:
-stripremoves metadata including GPS. Use-auto-orient -stripto preserve rotation. - Transparency: Converting PNG (with alpha) to JPEG loses transparency. Add
-background white -flatten. - Very large files: Process in batches to avoid memory issues.
- Already optimized: Check if compression actually reduces size — skip if negligible.
Security
- Sanitize filenames before processing — avoid shell injection via crafted filenames.
- Use
magicknotconvertto avoid path conflicts with Windowsconvert.exe.