Skip to main content

Compressing PNG images recursively with pngquant

Do you have a large amount of PNG images and need to reduce the file sizes? Try pngquant, it's great!

Especially for mobile apps and games it's really important to keep the final download size down. This because you can't rely on people around the globe having access to high-speed Internet connections and unlimited amounts of free mobile data.

In it's simplest form you can just run the following command to compress all the PNG images in the current working directory:

$ pngquant 256 *.png

One "problem" with this command is that it creates new image files instead of overwriting the existing ones. This might be nice sometimes, but is usually not what you want. To overwrite your files, just add the --ext and --force options like this:

$ pngquant --ext .png --force 256 *.png

Ok, that's nice, but what if you have your images structured in a directory tree? Going into each directory to run pngquant can quickly become a tiring task if you have more than just a few directories. So you just add the -r flag to run pngquant recursively, right? Nope, sorry, but there's no such option in pngquant. Don't worry, this is quite easy to overcome by using the find command in combination with pngquant.


$ find . -name '*.png' -exec pngquant --ext .png --force 256 {} \;

There you have it. Every PNG image in every sub directory will now be compressed. I've only tested this on macOS so far, but it should be possible to do the same on Windows.

I just did a recursive pngquant of my images folder of Ice Trap. Before compresson the directory size was 18.7 MB, and after pngquant had done its job the size was down to 8.9 MB. A decrease in size by 52% for a few seconds work. And no visible quality change in any of the images. That's just awesome!

Comments