ImageMagick convert & compress command generator
Convert between image formats with ImageMagick and compress properly. Correct flags for JPEG, PNG, WebP and AVIF, plus the fix for transparent PNGs turning black.
ImageMagick picks its output encoder from the file extension, not from a flag. Naming the output photo.webp is what selects WebP. Everything else is about telling that encoder how hard to work.
Optimise a JPEG for the web
magick input.jpg -strip -interlace Plane -sampling-factor 4:2:0 -quality 82 output.jpg
-stripremoves EXIF, comments and colour profiles.-interlace Planemakes it progressive, so it renders blurry-then-sharp instead of top-to-bottom.-sampling-factor 4:2:0halves the colour resolution. Invisible on photographs, but it smears fine coloured text and thin red lines — turn it off for screenshots and UI mockups.-quality 82is the usual sweet spot. Below about 75 artefacts become visible on skin and sky.
Transparent PNG to JPEG without a black background
The most-reported ImageMagick surprise. JPEG has no alpha channel, so when it is discarded the transparent pixels keep whatever RGB was stored underneath — usually black.
magick input.png -background white -alpha remove -alpha off output.jpg
-alpha remove composites against the background first; -alpha off then drops the now-redundant channel. -background must come before both, because it is a setting rather than an operator.
Convert to WebP
magick input.jpg -quality 80 -define webp:method=6 output.webp
method=6 is the slowest and smallest effort level. On photographs, WebP at q80 is roughly the size of JPEG at q65 for the same perceived quality. For graphics with flat colour, use lossless instead:
magick input.png -define webp:lossless=true output.webp
Shrink a PNG
magick input.png -strip -define png:compression-level=9 output.png
That is lossless. For a much bigger win on logos, screenshots and illustrations, quantise to a palette:
magick input.png -strip -colors 256 -define png:compression-level=9 output.png
Check what your build actually supports
Format support is compiled in, not enabled by a flag. Before debugging an AVIF or HEIC command, check whether your binary can write it at all:
magick -list format | grep -iE 'avif|heic|webp'
Each line ends with read/write permissions. If a format shows r-- you can read it but not write it, and no command-line option will change that — you need a build with the right delegate library.
A warning about -strip
-strip also removes the ICC colour profile. On images from a wide-gamut camera or a Display-P3 phone, dropping the profile without converting the pixels first makes colours visibly shift — usually flatter and duller. The safe order is:
magick input.jpg -colorspace sRGB -strip -quality 85 output.jpg