How to Set JPEG Compression in ImageMagick?

This article provides a quick overview and practical guide on how to adjust the JPEG compression quality level using the ImageMagick convert command. You will learn the exact syntax for the -quality setting, how it impacts file size and visual fidelity, and how to apply it to both single images and batch processing workflows.


The Basic Command Syntax

To set the JPEG compression quality in ImageMagick, you use the -quality option followed by a value from 1 to 100. A value of 100 represents the highest quality with the least compression (and largest file size), while 1 represents the lowest quality with the highest compression.

The standard command structure looks like this:

magick convert input.png -quality 85 output.jpg

(Note: In ImageMagick v7 and later, the convert tool is integrated into the main magick command, but magick convert or just magick works seamlessly).

Choosing the Right Quality Level

Finding the perfect balance between file size and image clarity depends on your specific use case. Here is a breakdown of how different quality ranges affect your images:

Advanced Quality Tweak: The -sampling-factor Option

When you compress a JPEG, ImageMagick also alters the color downsampling (chroma subsampling) to save space. By default, setting a quality below 90 triggers color subsampling. If you want to keep crisp text or sharp color edges even at lower qualities, you can explicitly define the sampling factor:

magick convert input.png -sampling-factor 4:2:2 -quality 80 output.jpg

Batch Processing Multiple JPEGs

If you need to apply the same compression level to an entire directory of images, you can use the mogrify command instead of convert. Be careful, as mogrify overwrites the original files unless specified otherwise.

To compress all JPEGs in a folder to 85% quality:

magick mogrify -quality 85 *.jpg