Get pngcrush for Mac OS X with or without Xcode
PNGcrush is an image optimization utility whose primary function is to reduce the overall file size of the PNG images in a lossless manner. It’s quite popular with developers and designers alike, and though it’s bundled in some versions of linux from the get-go, it’s not included in OS X by default without installing Xcode. We’ll cover four easy ways to get the utility on the Mac, with or without Xcode, and also offer an excellent free GUI alternative which may be more appropriate for many users.
ImageOptim, the Excellent pngcrush GUI Alternative
ImageOptim is an all-encompassing image optimization utility that also happens to integrate pngcrush within it’s capabilities. Because ImageOptim works beyond png files and also handles jpg and gif, it has long been our overall recommendation for compressing and optimizing images from OS X:
ImageOptim is very effective, and on average it reduces a file size of an image losslessly between 15-35%. It’s also an excellent tool to quickly strip EXIF data from image files, which is done simultaneously alongside file size reduction:
Those that are uncomfortable with the command line should stick with ImageOptim because it’s so easy to use, supports drag & drop and batch processing, and is basically foolproof. Nonetheless, there is also a command line version of ImageOptim-CLI available here, or those who are comfortable with the Terminal can proceed and use MacPorts or Homebrew to install pngcrush without ImageOptim.
Getting pngcrush with Xcode
If you have Xcode installed in OS X, you’ve already have pngcrush installed, it just happens to be buried fairly deep within the Xcode app package at the following location:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/pngcrush
That’s quite a ginormous path, so if you plan on using pngcrush directly you can either add that to your PATH directly, or make an alias for it within your .bash_profile:
alias pngcrush='/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/pngcrush'
Going this route obviously requires the installation of Xcode, which is fairly sizable, and if you’re not an iOS or Mac developer then it’s a bit excessive just to get a command line pngcrush tool, thus MacPorts and Homebrew are better options.
Install pngcrush in OS X with MacPorts
ImageOptim and ImageOptimCLI not doing it for you, and you don’t want to install Xcode? You can get pngcrush through MacPorts or Homebrew as well. Having MacPorts installed in OS X is obviously a prerequisite, if you don’t have it yet you can install it through source, a simple package, or svn directly from the developers.
sudo port install pngcrush
Installing pngcrush with Homebrew
For Homebrew users, installing pngcrush is simple as usual:
brew install pngcrush
Of course, you’ll need to install Homebrew first, which is easily done by executing the following command from the Terminal:
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
If you’re having trouble installing Homebrew or MacPorts, you probably haven’t installed Command Line Tools recently. Free and available from Apple, installing Command Line Tools separately requires a developer center login (the free variety works fine).
Using pngcrush from the command line
Regardless of having used Homebrew, MacPorts, or an Xcode alias to install pngcrush, using the tool is the same, and the basic format is like so:
pngcrush inputfile.png outputfile.png
The input file will not be modified, it will be duplicated as the output file given a different name like so:
pngcrush ~/Desktop/BloatedImage.png ~/Desktop/CompressedImage.png
pngcrush will provide a report of the compression, demonstrating the total file size reduction and how much CPU was used in the process:
Best pngcrush method = 10 (fm 5 zl 9 zs 1) for /Users/OSXDaily/Desktop/PngCrushTest.png
(29.90% IDAT reduction)
(25.23% filesize reduction)
CPU time used = 0.249 seconds (decoding 0.024,
encoding 0.217, other 0.008 seconds)
Much like ImageOptim, it’s very effective at reducing the file size of PNG documents:
Unlike ImageOptim however, pngcrush does not work on other image file formats.
“xcrun pngcrush” also works.
As Glenn mentions, “pngdefry”, although a useful tool results in png image sizes much larger than expected.
If image size is important, the pngcrush tool is a preferred option as it reverses Xcode processing of png files utilizing the “-revert-iphone-optimizations” (convert iPhone OS optimized PNG to a PNG readable on other platforms).
It might be worth pointing out that if you use the pngcrush that is included in Xcode, it’s different from the real pngcrush. It creates PNG files that are not really PNG files but “CgBI files”, that begin with a CgBI chunk and have reordered, premultiplied color samples. These files can be read by Xcode applications but not by regular libpng-based applications. There exists a 3rd-party “pngdefry” application that attempts to recover valid PNG files from a CgBI file.