
Pngquant – Lossy PNG compressor - pstadler
http://pngquant.org/
======
btbuildem
You should showcase at least a single sample of a compressed vs uncompressed
image right there on the first page, before the Features section.

------
alexdowad
WOW!!! I just applied this to the (big) PNG sprite files used in a project I
am working on now, and the gains in file size were amazing! And with hardly
any difference in quality!

Who ever said that reading Hacker News was a productivity drain?

~~~
nitrogen
Some alternatives while you're thinking about the subject: png-nq, pngcrush,
optipng. I'm sure there are others. If I want an image small, I try all of
them and pick the best.

~~~
pbhjpbhj
I don't know png-nq¹ but in their default use pngcrush and optipng aren't
[direct] alternatives to pngquant.

Optipng and pngcrush do lossless recompression and the program in the OP
AFAICT does lossy compression to create an image encoded in to PNG without an
intermediate representation.

As I understand it pngcrush will reduce the bit-size or apply a new palette if
that will reduce the file-size without loss of pixel data. Optipng can
optionally add lossy compression too.

¹ - [https://github.com/stuart/pngnq](https://github.com/stuart/pngnq) looks
like it was inspired by pngquant, from that page:

> _Pngnq exists because I needed a lot of png images in RGBA format to be
> quantized. After some searching, the only tool I could find that worked was
> pngquant. I tried pngquant but found that the median cut algorithm that it
> uses, with or without dithering, gave inferior looking results to the
> neuquant algorithm. You can see the difference demonstrated on the neuquant
> web
> page:[http://members.ozemail.com.au/~dekker/NEUQUANT.HTML](http://members.ozemail.com.au/~dekker/NEUQUANT.HTML)
> ._ //

~~~
riffraff
in my experiments pngnq worked better than pngquant but pngquant2 works better
than pngnq.

------
leeoniya
shameless self-promotion:

demo: [http://o-0.me/RgbQuant/](http://o-0.me/RgbQuant/)

repo:
[https://github.com/leeoniya/RgbQuant.js](https://github.com/leeoniya/RgbQuant.js)

* doesnt support alpha channel or dithering (yet)

~~~
Gravityloss
Very good, but looking at the examples, the smooth gradients look awful.

They would benefit from some noise to hide the transitions, but then the
compression would suffer.

Maybe some more palette entries for large smooth areas.

~~~
leeoniya
if you jack the minHueCols param up to 4096 or 8192, it does much better in
many cases of gradients but also much slower. it's certainly not a complete
hands-off quantizer. the best results i've found for 0-config quantizarion is
Wu's Color Quantizer v2 [1] implementation [2]

[1]
[http://www.ece.mcmaster.ca/~xwu/cq.c](http://www.ece.mcmaster.ca/~xwu/cq.c)

[2] [http://nquant.codeplex.com/](http://nquant.codeplex.com/)

~~~
pornel
There is a version of pngquant that uses Wu's algorithm:

[https://github.com/pornel/pngquant/tree/wucut](https://github.com/pornel/pngquant/tree/wucut)

However, Wu's algorithm requires preprocessing R * G * B * A * int array,
which is _16GB of RAM_ if done at full RGBA quality, so in practice all
implementations have to drop alpha and/or heavily posterize colors.

pngquant the same goal — subdivides RGBA (hyper)cube to minimize variance in
each section — but does it with much less memory and can do it at full
quality.

Posterization of input is my pet peeve, as it gives images slightly banded and
grainy look that we associate with "256-color" images (since VGA only ever
supported 6-bit per gun), and presume 256 colors are never enough for
photorealistic look — but it often _is_ , and people who use e.g. TinyPNG
service think it's magic.

~~~
leeoniya
yeah, i discovered how much space it needed when porting it via emscripten to
asm.js

~~~
pornel
It'd be awesome to port pngquant to JS, especially that JS is getting
parallelization and SIMD now.

Unfortunately I never got emscripten to work on my machine. Could you give it
a try and compile [http://pngquant.org/lib](http://pngquant.org/lib)?

~~~
leeoniya
sure i can give it a go. though i'm currently stuck in an airport going on
14h. i'll ping you when i get around to it but could be a few days

------
adamjernst
pngquant is great. I make an iPhone app that displays transit maps. Transit
maps are ideal candidates for quantization (limited color range, large blocks
of color that are perfect for RLE) and I saved a lot of file size by
quantizing the map tiles. Thanks pngquant!

~~~
SquareWheel
Sounds like vector images could be used effectively there.

------
seanalltogether
I've never quite understood how its possible to create better encoders without
needing a new decoder on the other end. Can anyone explain how this works?
When you're writing a png decoder, why wouldn't there just be one optimal png
encoder to go along with it?

~~~
HeyImAlex
For pngs (and lossless encoding) the thing is that the last step of encoding
is a run of the deflate algorithm. It's impossible to say what exactly a
stream will deflate down to without actually running your implementation of
deflate. Possible encodings of an image pre-deflation are staggering, 5^height
just for the filter step on all color types. I've been writing a brute force
compressor (everything but the deflate step which depends on the
implementation you choose) as a joke and it takes about an hour on images 10px
high. For that reason encoders use heuristics to make best guesses whenever
there's a choice to be made.

~~~
HeyImAlex
Oh and here's[1] a post I made about it. I decided to keep the project to
myself as I'm making it into a more, uh, realistic png compressor, but that
will give you an idea of all the possible descisions that need to be made
while encoding.

[1]
[http://heyimalex.com/journal/pngmassacre/](http://heyimalex.com/journal/pngmassacre/)

------
kraken-io
As kraken.io, we can safely say that this is indeed an excellent program, and
it makes up part of our lossy PNG optimization stack.

------
chipaca
How is this different from `pngnq` (that's been around for ages)?

~~~
saurik
If you use Google to ask this question the first hit is actually the pngnq
website, which explicitly states that pngnq is a modification of pngquant, and
supposedly an improvement.

[http://pngnq.sourceforge.net](http://pngnq.sourceforge.net)

> Pngnq is an adaptation by Stuart Coyle of Greg Roelf's pnqquant using
> Anthony Dekker's neuquant algorithm.

> Why another quantizer?

> Pngnq exists because I needed a lot (several thousand) of png images in RGBA
> format to be quantized. After some searching, the only tool I could find
> that worked for RGBA was pngquant. I tried pngquant but found that the
> median cut algorithm that it uses, with or without dithering, gave inferior
> looking results to the neuquant algorithm. You can see the difference
> demonstrated on the neuquant page.

~~~
leephillips
I compared them recently for a project where I needed to make some PNGs
smaller, and settled on pngng, which, in my informal testing, seemed to
produce better results.

~~~
pornel
Make sure you haven't tested the 13-years-old pngquant 1.0 that is shipped
with Debian stable and some other distros.

pngquant2 should clearly beat pngnq.

~~~
leephillips
I tested v. 1.0-4.1 that comes with Ubuntu 12.04.1 (I think). Am I missing
out?

~~~
pornel
Yes. 1.0 was written in the MS-DOS era, truncates quality to 15-bits, has
buggy alpha and uses basic Median Cut algorithm from early 80s.

2.0 is by now a totally different program, with expanded and improved
algorithms (with bits of machine learning) and uses 128 bits per pixel
precision (SIMD+multicore optimized).

------
faxenoff
How about Color Quantizer? I use it for years.
[http://www.softpedia.com/get/Multimedia/Graphic/Graphic-
Othe...](http://www.softpedia.com/get/Multimedia/Graphic/Graphic-Others/Color-
quantizer.shtml)

Offical site [http://x128.ho.ua/color-quantizer.html](http://x128.ho.ua/color-
quantizer.html)

------
xlevus
Some examples would be nice.

~~~
codelike
This page has some comparison examples:

[http://bugfixer.endel.me/2012/09/23/comparison-between-
png-q...](http://bugfixer.endel.me/2012/09/23/comparison-between-png-
quantizers/)

------
Gigablah
This is how I use pngquant in my thumbnailing workflow:
[https://gist.github.com/gigablah/3110899](https://gist.github.com/gigablah/3110899)

At 100x100 pixels, I find that this produces small (~8kb) PNG thumbnails which
are still sharp with no noticeable color loss.

(Disclaimer: I'm a beginner at nodejs)

------
pstadler
Available in Homebrew:

    
    
        brew install pngquant

------
j_s
Read more about the details of implementing something like this in C#:

[http://www.mattwrock.com/post/2011/09/05/Convert-32-bit-
PNGs...](http://www.mattwrock.com/post/2011/09/05/Convert-32-bit-PNGs-to-high-
quality-8-bit-PNGs-with-C.aspx)

------
kristofferR
The ImageOptim/ImageAlpha OS X apps are fantastic for compressing all kinds of
images for the web.

[http://imageoptim.com/](http://imageoptim.com/)
[http://pngmini.com/](http://pngmini.com/)

~~~
amadeus
It should be noted that ImageAlpha, the app under the pgnmini.com url,
provides a GUI to to PNGQuant and PNGNQ.

While a command line tool is useful by itself, using lossy compression is best
done in a GUI to determine how much compression draws the right balance
between file size and its loss in quality. ImageAlpha is amazing for this.

------
zw123456
Very timely, I was just working on a page that had to display complex graphs
and the regular PNG's were huge. I set the quality to 50% and it reduced the
size to about 30% of the original and it still looks good. Thank you!

------
webjunkie
Use ImageAlpha as a GUI for OSX. Very nice results!

[http://pngmini.com/](http://pngmini.com/)

------
blueskin_
Lossy PNG compression is like trying to use a Ferrari to pull a 35 tonne
trailer.

PNG is for graphics; JPEG is for photos.

------
ericthegoodking
I like this small program , great for compressing png files. Congrats dev.

------
frncscgmz
Anyone knows of a similar tool for jpeg images?

~~~
blacksmith_tb
Jpegs are already lossy, but if you want to increase the lossiness (and
decrease filesize)via the commandline, you can do that with imagemagick or
graphicsmagick with the -quality flag on a convert.

------
noiv
Any link to the 2.0 32bit deb?

~~~
pornel
Debian (unstable) has an up-to-date package now:
[http://packages.debian.org/sid/pngquant](http://packages.debian.org/sid/pngquant)

~~~
noiv
thx.

------
rockdoe
The algorithms link is broken.

