10 options for pngquant image compression utility

Introducing pngquant, an image compression utility. Do you have any concerns about image size? In particular, SEO optimization or game developers should minimize the size of the image files above all else, right?

What is pngquant?

pngquant is a lossy PNG compressor based on the libimagequant library, created by Kornel Lesiński with several other contributors. Basically, it is a structure that reduces capacity while incurring loss in images. More efficient than 24-bit or 32-bit PNG files, converting to 8-bit PNG format with an alpha channel can save 60-80% of its capacity with some loss in the image. Compressed images are standard compliant and can be used in both web browsers and operating systems.

Programs using pngquant

GUI Programs

Unfortunately, there are no GUI applications for Linux using pngquant, but those available for MacOS and Windows include ImageOptim, Pngyu, PNGoo, and the plug-in SuperPNG for Adobe Photoshop.

Figure 1. GUI application for Windows for Mac with image compression utility pngquant
Figure 1. GUI application for Windows for Mac with image compression utility pngquant

Application for Command-line

Ubuntu

In Ubuntu, you can install the utility with one line of command below.

sudo apt install pngquant
ShellScript

MAC, Windows

Binaries for MacOS and Windows can be downloaded from the pngquant homepage.

Build Source Code

You can also get the source code from pngquant’s github and compile it yourself. Unless you absolutely have to, we recommend that you just use the distribution.

Usage

We will omit the description of the application for GUI. Here, we will take a look at the options of pngquant, an application for cli.

Help

First of all, you can check the description of all options by entering the following. You can check the description just by typing pngquant. You can also use –help instead of -h.

pngquant -h
ShellScript

–ext extension option

The –ext option determines the extension of the converted filename after the existing filename. If you add it with an option such as –ext -new.png, for example, if the original image file is abcd.png, the converted result will be saved in the format of abcd-new.png.

–quality min-max or -Q min-max

min and max take values from 0 (worst) to 100 (perfect). The max value forces the use of the minimum number of colors required to meet or exceed the image quality. And if the conversion result is better than the quality of the min value, it is not saved and exits with a status value of 99. For example, you can use –quality=65-80.

–speed N option

N takes a value from 1 to 10, where 1 produces the highest quality results but is slow, and 10 produces 5% lower quality but is 8x faster. 11 disables dithering and lowers the compression level. In other words, there is a trade-off between speed and quality.

–strip option

Remove optional metadata.

–nofs option

Disable Floyd-Steinberg dithering.

–floyd=N option

You can set the level of dithering. N is 0 for no dithering and 1 for maximum dithering. = is required. And the N value can be set to any value with a decimal point between 0 and 1.

–posterize bits option

You can set how many of the least significant bits of a color are clipped per channel. Used when the image is displayed on a screen with low depth. pngquant makes nearly opaque pixels fully opaque, and reduces the amount of translucent colors. The bits value can be from 0 to 4. In the case of 4, a lot of clipping occurs, resulting in a lot of damage to the image. Instead, the capacity is reduced.

–force option

The output file is overwritten if it exists. Also used as -f.

–skip-if-larger option

If the conversion results in a file that is larger than the original file, do not save the image.

Example usage of options

Compress the image by reducing the number of colors to 64

If you write as below, the resulting image will be saved as an image with 64 colors. Since no extension was specified, it is saved as image-fs8.png.

pngquant 64 image.png
ShellScript

If the quality is good, compress the image and overwrite the original

If you can scale it down without losing too much quality, it’s just an overwrite command over the original.

pngquant -f --ext .png --quality 70-95 image.png
ShellScript

Challenging settings create the most desired results

I want the quality to be without great loss, the dithering to be medium, the least significant 1 bit to be cut off, the compression to be maximal, the color to be 128 colors, and the extension of the converted file to be image-re.png. And if you want to overwrite the same file even if you print multiple times, you can run the following command.

pngquant -f \
  --ext -re.png \
  --quality 70-95 \
  --posterize 1 \
  --floyd=0.5 \
  --speed 1 \
  128 image.png
ShellScript

I don’t know how to do it best, but if you adjust it well, it seems that you can create a level similar to services like tinyPNG.

Example of image compression using pngquant

The top one is the compressed image, the bottom one is the original image.

In the case of this image, I thought that there were not many colors and it was simple, so I ran pngquant 32 image.png, and this result came out. In the case of an image that does not need to express a lot of color, it does not seem too difficult to compress the image by lowering the number of colors. The capacity has been drastically reduced, down to about 23%.

Figure 2. image with and without image compression applied
Figure 2. image with and without image compression applied

Reference

Please refer to the pngquant homepage for more information.
Lou Park’s blog has a retrospective record of making TinyDoge, a lossy compression program(Korean article).

1 thought on “10 options for pngquant image compression utility”

Leave a Comment