Tutorial · images · Published 2026-08-16 · 2 min read
Image optimisation with command line tools
Command line image optimisation: cwebp, ImageMagick, oxipng and squoosh-cli commands that shrink images before upload.
Choose the tool per format
A command line is the fastest way to compress an image consistently, especially in a build script or a content pipeline. Pick the tool by the format you need to produce:
- WebP with
cwebp(from the libwebp tools) is a common output that balances size and quality. - AVIF can be produced with
avifencfrom libavif, usually by first converting from a source. - PNG with
oxipngorpngquantreduces size without the lossiness of a full format switch. - JPEG and SVG resizes are handy with ImageMagick (
magick) or the lighter GraphicsMagick.
# To WebP at quality 80
cwebp -q 80 input.jpg -o output.webp
# Resize and re-compress to JPEG, width 1280, quality 82
magick input.jpg -resize 1280x -strip -quality 82 output.jpg
A resize and compress pipeline
In a script, process in that order: decode once, strip metadata, resize, then encode to your target format.
# One shot: convert source to a 1280 wide WebP, strip metadata, quality 80
cwebp -resize 1280 0 -metadata none -q 80 input.png -o output.webp
Batching in a loop with a small shell line keeps every file consistent:
for f in *.PNG; do
magick "$f" -resize 1280x -strip -quality 82 "${f%.PNG}.webp"
done
| Step | Tool example | Result |
|---|---|---|
| Strip metadata | -strip (magick), -metadata none (cwebp) | Removes EXIF/GPS |
| Resize | -resize 1280x | Matches display size |
| Compress | -quality 82 | Shinks bytes |
| Modern format | cwebp, avifenc | Fewer bytes at similar quality |
Verify the output
Compression is only useful if the bytes actually drop. Compare before and after, and confirm the image still matches its display size:
ls -la input.jpg output.webp
identify -verbose output.webp | grep -E 'Geometry|Filesize'
- If the output is close to or larger than the input, raise compression, reduce the dimensions, or choose a different format (for example a photographic image as WebP/AVIF instead of PNG).
- If a PNG with transparency becomes larger as JPEG, keep the format that preserves what the image needs.
The image optimisation guide covers the full format and size decisions, the compression guide explains quality trade-offs, and once you have AVIF-capable output the AVIF pipeline shows how to serve it alongside WebP. A WebP reference decides between the two modern formats before you commit the pipeline.