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:
- 95–100 (Maximum Quality): Best for archiving or print. File sizes remain large because almost no visual data is discarded.
- 80–90 (High Quality): The sweet spot for web use. It significantly reduces file size while remaining visually indistinguishable from the original to the human eye. Quality 85 is widely considered the industry standard default.
- 60–75 (Medium Quality): Ideal for thumbnails or content where fast loading speeds are critical and minor compression artifacts are acceptable.
- Below 60 (Low Quality): Noticeable pixelation and “blocky” artifacts will appear. Only recommended for extreme storage constraints.
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