DEV Community

junyu fang
junyu fang

Posted on

Top PNG compression methods on macOS compared — are native APIs useless?

Hello everyone, I'm an Apple developer from China. When I upload blog posts, I usually need to compress images, so I use TinyPNG to compress images. However, TinyPNG requires the use of the Internet. When the network signal is poor or when I need to compress private images, I am always worried that the images will not be compressed properly.

Then I started looking for ways to compress PNG images. On the Mac platform, there are several options for best practices and third-party libraries for compressing PNG images:

  1. pngquant: Color quantization, lossy compression, CLI/C API support, best choice (compression up to 70-90%).

  2. ImageOptim CLI: A collection of tools, lossy compression, CLI support, calling pngquant/optipng/zopfli, etc.

  3. optipng: Reordering and deduplication, lossless compression, CLI support, typically 5-20% compression.

  4. TinyPNG: Color analysis and compression, lossy compression, HTTP API support, good results, free with usage limits.

  5. NSBitmapImageRep: Re-encode, lossless compression, native to macOS, almost no compression.

  6. Image I/O (CGImageDestination): Write to PNG, lossless compression, native to macOS, also almost no compression.

Test plan

I tried testing all the solutions using this PNG image (1.9MB):

1. pngquant

By default, pngquant can achieve a compression rate of 89%.

If the configuration parameter is set to 0-1, the maximum compression can be 99.01%.

A 1.9MB image can be compressed to 17KB at the maximum, with a compression rate of 99.01%.

2. ImageOptim

ImageOptim compresses the file size by 54% by default, which is a medium level.

After turning on the lowest compression configuration.

PNG compresses 96.5% of its volume, and PNG becomes 66KB from 1.9MB, which is also a very high compression ratio.

3. optipng

optipng can set filters and compression strategies, but optipng can only compress up to 51.5%.

Moreover, each layer of compression takes a long time to calculate, which is more time-consuming than the previous methods.

4. TinyPNG

TinyPNG compresses files by 75% by default.

5. NSBitmapImageRep

Mac's native image compression API can compress image size by 35%.

6. CGImageDestination

Similar to NSBitmapImageRep, both are compressed by 35%.

Test Results

Among all the solutions:

  1. pngquant and ImageOptim achieved the highest compression ratios, achieving minimum compression ratios of 99% and 96.5% respectively;

  2. tinypng achieved a compression ratio of 75%;

  3. optipng achieved a compression ratio of 51.5%;

  4. NSBitmapImageRep and CGImageDestination achieved a compression ratio of 35%.

TinyPNG, a free web-based PNG image compression tool I frequently use at work, boasts very high compression rates. However, it's not usable on a local computer.

NSBitmapImageRep and CGImageDestination are built-in image compression methods on Mac. They offer the worst compression rates for PNG images. However, if you don't want to use third-party packages, you can consider compressing images using NSBitmapImageRep and Image I/O (CGImageDestination). Compared to these two methods, CGImageDestination supports compression of EXR and HEIC files and prevents reverse compression (where a smaller image is compressed into a larger one).

Local Image Compressing
For average users who want to compress PNG images locally on a Mac, ImageOptim with its visualization window is the only option.

pngquant, optipng, TinyPNG, NSBitmapImageRep, and CGImageDestination require a certain level of technical knowledge.

  1. pngquant needs to be installed and compressed through CLI:

brew install pngquant

pngquant --quality=65-80 --output output.png your_image.png

  1. optpng also requires CLI installation and configuration of compression parameters:

brew install optipng

optipng -o7 your_image.png // -o7 indicates the strongest compression level

  1. TinyPNG requires a network connection and cannot natively compress images.

  2. NSBitmapImageRep and CGImageDestination are both native compression APIs and cannot be used directly.

So, if you want to use a native Mac API + PNG compression, which one should you choose?

ImageSlim

ImageSlim uses Mac's native image compression API CGImageDestination, which supports compression of image formats such as HEIC, EXR, JPG, JPEG, and PNG, but is not as good as pngquant and ImageOptim in PNG compression.

Because the Mac's native compression API lacks sufficient advantages for PNG, ImageSlim has integrated pngquant. You can enable the pngquant third-party library in the settings to achieve PNG compression, switching back and forth between the Mac's native API and the third-party library.

The Mac's native compression API provides excellent support for JPG, JPEG, HEIC, and EXR compression scenarios, while pngquant complements its shortcomings in PNG image compression, creating a perfect combination of the two.

Why choose ImageSlim? Because ImageSlim is a free, open-source image compression tool. You can enable/disable third-party image compression engines. If you don't enable third-party image compression engines, it defaults to using the native Mac compression API to ensure compatibility and speed.

ImageSlim compresses images entirely on your local computer and promises not to upload your images to third-party services or collect your private data.

ImageSlim is an image compression tool designed for efficiency, privacy, and simplicity, and is used by developers, designers, content creators, and anyone looking to reduce image size.

ImageSlim GitHub link: https://github.com/fangjunyu1/ImageSlim

Mac App Store: https://apps.apple.com/cn/app/%E8%BD%BB%E5%8E%8B%E5%9B%BE%E7%89%87/id6748277056?mt=12

The Mac App Store link is for the Chinese region and may not open. Please search for "ImageSlim" on GitHub or the Mac App Store to download it.

Related Articles

  1. pngquant:https://pngquant.org/

  2. ImageOptim:https://imageoptim.com/mac

  3. opt PNG:http://optipng.sourceforge.net/

  4. tinify:https://tinify.com/developers

  5. tinypng:https://tinypng.com/

  6. GitHub link: https://github.com/fangjunyu1/ImageSlim

  7. Mac App Store link for ImageSlim: https://apps.apple.com/cn/app/%E8%BD%BB%E5%8E%8B%E5%9B%BE%E7%89%87/id6748277056?mt=12

Top comments (0)