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:

# 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
StepTool exampleResult
Strip metadata-strip (magick), -metadata none (cwebp)Removes EXIF/GPS
Resize-resize 1280xMatches display size
Compress-quality 82Shinks bytes
Modern formatcwebp, avifencFewer 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'

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.

Need a website built, fixed, optimised, migrated or replaced?

This technical resource is written by CSMBAC, a small design and development studio. If you would rather hand the problem to a professional, the website service page explains how we build enquiry-ready websites.

Explore website services