Reduce PNG Graphic Size Using PNGCRUSH

By  on  

Now that I'm knee-deep in MacBook I love working with command line applications. There's a certain beauty in the simplicity of using the console instead of a nice GUI. One task I use the console for often is reducing the size of PNG files using the powerhouse that is PNGCRUSH.

The Shell Script

# directive
pngcrush -reduce -brute source.png destination.png

#result
Best pngcrush method = 113 (fm 0 zl 9 zs 0) for destination.png
     (33.38% IDAT reduction)
     (33.43% filesize reduction)

   CPU time used = 835.707 seconds (decoding 151.450,
          encoding 679.145, other 5.112 seconds)

The above directive is a basic usage of PNGCRUSH. PNGCRUSH offers a wealth of options that allow you to optimize your PNGs. If you use many PNGs on your website, optimizing them using PNGCRUSH is a must. Using the above directive, PNGCRUSH took a 11.7MB down to 7.8MB. NO QUALITY LOSS. Sure it took over 10 minutes, but think of the bandwidth your site will save.

Recent Features

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

Discussion

  1. no way dude, I suggest u 2 give a try to image magick
    that looks a little bit powerful & faster and gives u the same or better results

    http://www.imagemagick.org/script/index.php

    when I need to shrink down images in some app I always use this
    it’s capable to manage even other formats such as jpg, gif etc..

    • Could you elaborate on how image-magick can compress PNGs? I can’t seem to see anything in the documentation on how to do it. It is an amazingly powerful program, so I am sure I am missing something.

  2. Fabian
  3. I’ve always loved PNG’s – and I’ve been impressed with the file sizes that Fireworks and Photoshop can output. But thx to this heads up David it looks like I’ll be squeezing a little bit more out of my PNG’s from now on.

    Cheers

    And apparently the above is spammy and I have to try again… what algorithm are you using? I have no links… It’s a pretty straight up comment…

  4. I’ve used that proggy in the past and it gave me same results as photoshop pics.
    Whenever I’ll need to try something more than imagemagick, I’ll give it a try…

  5. And if anyone doesn’t want to install pngcrush or mess with terminal, they can use this dashboard app which does the same thing: http://www.plasticvicar.com/PNGpong/
    You just drop the image onto it
    I did a quick test with pngcrush/terminal and pngpong – result was the same

  6. Simon

    Smush.it is indeed a very good compression tool, but sometimes tools like pngcrush or optipng do a better job.
    I use optipng and it’s damn good ! Yes it’s slow but it works really great. On Windows (yes I know, shame on me :p ) I use a .bat file with this in it:
    “PathTo\optipng.exe” -o7 %1
    Drag & drop and you’re done :)

  7. I’ll do a follow up post where I try all of these methods and see which does the best job. Good suggestions everyone!

  8. i like pngout with .cmd file:

    for /F “delims=” %%S in (‘dir /S /A-D /B *.png’) do @pngout “%%~S”

    with recursive file scan in subdirs :D

  9. James Parsons

    I prefer http://www.smush.it as well. It uses several compression methods to get the smallest possible file size without losing any quality. The difference between smush.it and pngout/pngcrush is ~0.1KB from my tests, and smush.it is more efficient.

  10. Anton

    @kburn

    I’ve tried both scripts, PNGCRUSH and IMAGEMAGICK on a 330kb ‘photobooth’ photo and PNGCRUSH was ultimately better:
    PNGCRUSH = 260kb
    IMAGEMAGICK = 276kb

  11. In your comparison make sure to compare “Save for Web” in photoshop. I’m curious to see how it stacks up.

    I’ve been using macs for over 10 years now and I’m still terminal averse.

  12. @Ryan Florence,

    I have compressed images with png crush over 90% of that of Adobe’s “Save for Web”.

    http://www.marksanborn.net/howto/compress-images-for-faster-load-times/

    @David,

    Checkout jpegtran for Jpegs. Also, imagemagic is awesome but I usually use it for a different purpose. For example, image magick can tell you how many colors your image has and you can adjust your png accordingly. Compressing a 24bit png with pngcrush is worthless when you only use 8 colors in your image.

  13. Gracepoint After Five just recently launched punypng (http://www.gracepointafterfive.com/punypng) which combines a lot of the open-source technologies out there. The Ask.com User Experience team that I’m a part of uses punypng as our weapon of choice as well as the Gracepoint Fellowship Church web team. It’s very competitive to smush.it and pngout, and punypng beats them out most of the time (though don’t my word for it, try it out yourself to see).

    I think the great thing about something like punypng versus the command-line tools is that you can throw a whole folder at punypng and just let punypng do all the work to figure out which tool is best for the job. Sometimes converting a JPEG to PNG and then compressing it down is better … sometimes jpeg-tran or jpegoptim is better. Punypng just works.

    For more info about punypng on the Gracepoint After Five blog:
    http://www.gracepointafterfive.com/punypng-making-the-web-a-more-puny-place

  14. DivaWebDzgn

    This is why I always say, you NEVER stop learning.. I thought I “got it” when it came to graphics optimization.. Been Googling my but off and found out that there’s a whole WORLD of information I didn’t know about.. Especially when it comes to my beloved PNGs.. LOL Good article!

    I’ll be watching this blog for the follow-up article.. I’m curious as to how http://www.plasticvicar.com/PNGpong/ stacks up.. I’m a lot like Ryan.. I’m not necessarily terminal adverse, but after years of Macs and WIndows machines, I just prefer a GUI app over command line app.. :)

  15. This is great, but, what about Windows users?

  16. Hassan: Grab Cygwin and install PNGCRUSH on that. Or you could just use Smush.It.

  17. Paul de Vries

    I tried PNGCursh and Smush.it on an uncompressed PNG of 93Kb.

    Results
    PNGChrush: 90Kb
    Smush.it: 87,32Kb
    PS Save for web: 87,06Kb

    I used the following command line for pngcrush: pngcrush.exe -reduce -brute -e 2.png “%1”

    Maybe I am doing something wrong?

  18. @paul: Try punypng.com

  19. I did a comparison of 8 free image optimizer tools, the post is in Spanish but the comparison chart is very easy to understand.

    http://www.bitacoradewebmaster.com/2009/10/02/8-optimizadores-de-imagenes-para-web/

  20. So… there a way for non-command line folks to do this via a drag / drop app?

  21. @David Walsh: pngcrush works just fine on Windows too.

    http://pmt.sourceforge.net/pngcrush/

  22. @jay: Acorn on the Mac uses pngcrush when you save a png for the web.

  23. Greg ONeill

    Not sure if it’s because of a mac or something but I don’t think it should take 10 minutes. On my windows machine the operation was almost instant.

  24. Otamay

    A little late (half year xD).

    The script is very useful for using pngcrush. I’m using it to reduce the size of hi resolution zelda images. Yeah!

    But I’ve had problems with file names containing spaces. This little modification can do the trick.


    #!/bin/sh
    find $1 -name "*.png" | while read png
    do
    echo "crushing $png"
    pngcrush -brute "$png" /tmp/temp.png
    mv -f /tmp/temp.png "$png"
    done;

  25. Tarik

    I use http://optipng.sourceforge.net/ and it is kind of cool enough for me. :)

  26. A nice idea for handling a lot of PNG in the context of website development would be to dynamically separate the image data from the alpha channel on the server, optimize both of them separately and recombine them using the canvas element and some JavaScript in the browser. Ta-Da! Works with all modern browsers.

    Here is explained how it’s done:

    http://headers-already-sent.com/artikel/shrinkimage-1/

    You’ll also find a complete ZIP package with the PHP script and a jQuery plugin. Let us know what you think about this.

  27. What about http://tinypng.org/

    Has anyone tried it.

  28. Tom

    @parvez, http://tinypng.org/ works wonders.. PNGCrush & Smush.it could reduce my image down to 23KB from 25, PunyPNG offered savings up to 16KB with lossy compression if I upgraded to their ‘pro plan’, however TinyPNG brought it down to 8KB with lossy compression and it’s a free service. It preserved full alpha transparency and my eyes cannot spot a single difference from the original. Thanks for the recommendation, using this site from now on and donating soon.

  29. Tom

    I would like to add that Yahoo’s “smush.it” is great for losslessly reducing images, e.g. JPEG ;)

  30. ddd

    I can recommend this service:
    http://tinypng.org/

    They strip off meta data but too compress the image data as well. purists may complain, but the results are very good, in deed, for file size and image quality…

    Source Image/No Alpha/24-Bit PNG (401,9 KB):

    216,3 KB with tinypng.org
    375,7 KB with PNGpong
    381,3 KB with Yahoo Smush.it

    @Conrad Chu:
    Your service isnt free for image sizes above 150KB :-( You are inventor of this software and also running this paid service. So I have to consider your comment here to be spam?

  31. I can recommend this service:
    http://tinypng.org/

    They strip off meta data but too compress the image data as well. purists may complain, but the results are very good, in deed, for file size and image quality…

    Source Image/No Alpha/24-Bit PNG (401,9 KB):

    216,3 KB with tinypng.org
    359,5 KB with pngCrush (ImageOptim, OS X-GUI for pngCrush)
    375,7 KB with PNGpong
    381,3 KB with Yahoo Smush.it

    @Conrad Chu:
    Your service isnt free for image sizes above 150KB :-( You are inventor of this software and also running this paid service. So I have to consider your comment here to be spam?

    PS: Why cant I post a comment here? No feedback on user action??!

  32. Pol

    Try http://compresspng.com
    It’s much better than tinypng.

  33. Tom

    Hey guys, TinyPNG uses http://pngquant.org/ which has a GUI port for windows called PNGoo.
    Also check out http://kraken.io/ for any lossless optimization.

  34. copyhold

    I must say that GIMP beats them all. At least on small images (like those that you convert to datauri)
    photoshop gave me 900 byte
    imagemagik – 300
    pngcrush -300
    GIMP – ….. 120!!!!

    just do
    $gimp 1.png
    ALT-f w CTRL-Q

  35. d

    another vote for http://tinypng.org/

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!