imagemagick

Why does ImageMagick setImageCompressQuality 0 result in a larger file size than 1?


I'm converting PNG images to WebP and trying to do maximum compression with ImageMagick.

My PHP code to run ImageMagick looks like this:

$imagick = new Imagick($filePath);

// Set WebP format and quality
$imagick->setImageFormat('webp');
$imagick->setImageCompressionQuality(0);

// Set WebP-specific compression parameters
$imagick->setOption('webp:method', '6'); // Maximum compression speed (0-6)

// Remove unnecessary metadata to reduce size further
$imagick->stripImage();

$outputFile = $outputDir . '/' . $fileName . '.webp';
$imagick->writeImage($outputFile);

From a 600 KB PNG source file, this produces a 217 KB WebP file.

However, when I do this conversion manually in PixelMator Pro on macOS, I get a file size of just 45 KB ("Export for Web" as WebP with quality of 0, no other settings). When I open the 217 KB WebP file generated by ImageMagick in PixelMator Pro and use "Export for Web" with quality of 0, I get a file of 56 KB. So I wondered why PixelMator is so much more efficient than ImageMagick.

So I tried setImageCompressionQuality(1), and then ImageMagick produced a file of 58 KB, effectively the same as PixelMator.

Why does ImageMagick produce larger file sizes for 0 quality? I confirmed this behavior on ImageMagick 7.1.1-41 on macOS and 6.9.11-60 on debian.


Solution

  • In my experience with ImageMagick, setting the compression quality to 0 does not indicate the highest quality or maximum compression. It usually indicates the fastest, leading to larger file sizes because of inefficient compression algorithms.

    Typically, 1 is the lowest quality and highest compression. I couldn’t find specific documentation on WebP quality values, but the JPEG documentation mentions that a quality setting of 1 is the lowest quality level.

    Based on imagemagick docs:

    For the JPEG and MPEG image formats, quality is 1 (lowest image quality and highest compression) to 100 (best quality but least effective compression). The default is to use the estimated quality of your input image if it can be determined, otherwise 92. When the quality is 90 or greater, then the chroma channels are not downsampled. Use the -sampling-factor option to specify the factors for chroma downsampling.