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:
pngquant: Color quantization, lossy compression, CLI/C API support, best choice (compression up to 70-90%).
ImageOptim CLI: A collection of tools, lossy compression, CLI support, calling pngquant/optipng/zopfli, etc.
optipng: Reordering and deduplication, lossless compression, CLI support, typically 5-20% compression.
TinyPNG: Color analysis and compression, lossy compression, HTTP API support, good results, free with usage limits.
NSBitmapImageRep: Re-encode, lossless compression, native to macOS, almost no compression.
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:
pngquant and ImageOptim achieved the highest compression ratios, achieving minimum compression ratios of 99% and 96.5% respectively;
tinypng achieved a compression ratio of 75%;
optipng achieved a compression ratio of 51.5%;
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.
- pngquant needs to be installed and compressed through CLI:
brew install pngquant
pngquant --quality=65-80 --output output.png your_image.png
- optpng also requires CLI installation and configuration of compression parameters:
brew install optipng
optipng -o7 your_image.png // -o7 indicates the strongest compression level
TinyPNG requires a network connection and cannot natively compress images.
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
pngquant:https://pngquant.org/
ImageOptim:https://imageoptim.com/mac
opt PNG:http://optipng.sourceforge.net/
tinypng:https://tinypng.com/
GitHub link: https://github.com/fangjunyu1/ImageSlim
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)