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.

3
  • A tool that reports the number of distinct colors in the image colors may give you the beginning of an answer. The RGB histogram could also help. At some point it's not the compression settings that are important, it is what has been done to the image beforehand to make the compression work better, and this often includes some reduction in colors (color indexing...). Commented Dec 9, 2024 at 17:48
  • You're more likely to get a useful answer if you share your starting PNG image and your ImageMagick and PixelMator output images - using a service that doesn't alter images - such as Dropbox or Google Drive. Commented Dec 10, 2024 at 9:34
  • Thanks, I updated the question-- I managed to narrow down the problem to using setImageCompressionQuality(0) producing large files-- setting the quality to 1 or 2 or 5 produces much smaller files. Commented Dec 11, 2024 at 1:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Huh, that's interesting that 0 is a completely different process in ImageMagick, whereas in other tools like PixelMator and squoosh.app 0 is just the lowest value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.