In this guide, we are going to learn what is Pngquant and its features, how to install Pngquant and how to compress PNG images on Linux using Pngquant.
Table of Contents
What Is Pngquant?
Pngquant is a free, open source and cross-platform command-line lossy PNG compressor. It is based on a portable libimagequant library and is written in C99.
It reduces the file size significantly by converting the PNG image to more efficient 8-bit PNG format and preserves full alpha transparency. As you may already know, 8-bit PNG files are often 60-80% smaller than 24/32-bit PNG files.
The images compressed using Pngquant are fully-compatible with all web browsers and operating systems. Pngquant can compress one or multiple images at once.
Features
Pngquant has the following features.
- Support for batch conversion of multiple PNG files.
- High-quality palette generation.
- It uses Median Cut quantization algorithm to optimize images.
- Can be easily integrated with shell scripts, GUIs and server-side software.
- Convert images in desired quality and skips images which can't be converted with the desired quality.
- Multicore support and Intel SSE optimizations.
Install Pngquant in Linux
Pngquant can be installed in many ways.
Install Pngquant from distribution's repositories
Pngquant is available in the default repositories of most Linux distributions.
On Arch Linux and its variants, Pngquant is available in [Community] repository. Enable Community repository and install it using Pacman like below.
$ sudo pacman -S pngquant
On Debian, Ubuntu, Linux Mint:
$ sudo apt install pngquant
On Ubuntu systems, you need to enable [Universe] repository to install Pngquant.
To enable Universe repository, do:
$ sudo add-apt-repository universe
On Fedora:
$ sudo dnf install pngquant
On openSUSE:
$ sudo zypper install pngquant
On CentOS:
$ sudo yum install epel-release
$ sudo yum install pngquant
Check the installed Pngquant version:
$ pngquant -V 2.5.0 (June 2015)
The version of Pngquant available in default repositories might be old. If you want the most recent version, compile and manually install it from source like below.
Install Pngquant from source
First, install install the necessary dependencies.
On Debian, Ubuntu:
$ sudo apt install git gcc cmake libpng-dev pkg-config
On CentOS, Fedora:
$ sudo yum install git libpng-devel gcc cmake
Next, git clone the latest version of Pngquant:
$ git clone --recursive https://github.com/kornelski/pngquant.git
The above command will download the contents of Pngquant GitHub repository and save it in a folder named "pngquant" in your current working directory.
Switch to the "pngquant directory:
$ cd pngquant
Finally, run the following commands to compile and install Pngquant:
$ make
$ sudo make install
Pngquant will be installed in /usr/local/bin location by default. If you want to install it in another directory run:
$ ./configure --prefix=dir && make
Replace "dir" with your path. You need to run this command first then run and "sudo make install" command.
Check Pngquant version:
$ pngquant -V 2.12.5 (July 2019)
See? It is the most recent version.
Install Pngquant using Cargo
Pngquant can also be installed using Cargo package manager. First, install Rust as described in the following link.
git clone the latest version of Pngquant:
$ git clone --recursive https://github.com/kornelski/pngquant.git
Finally, build and install Pngquant using Cargo like below.
$ cd pngquant
$ sudo cargo build --release
For other installation methods, please refer Pngquant official installation page.
Compress PNG Images On Linux Using Pngquant
Pngquant usage is trivial and doesn't require any special skill to use it.
I have PNG image file named "ostechnix.png" in my current directory. Let me show you its size.
$ du -sh ostechnix.png 152K ostechnix.png
As you can see, the image size is 152k.
Compress a PNG image
To compress a PNG image with Pngquant, just provide its path like below:
$ pngquant ostechnix.png
The above command will compress ostechnix.png file and save it as a new file with name "ostechnix-fs8.png". By default, Pngquant will use output filename same as input filename with suffix -or8.png or -fs8.png at the end. Meaning it will not overwrite the source file. If you're not happy with the compression rate, simply delete the compressed file and keep the original one.
Let us again check the compressed image file size:
$ du -sh ostechnix-fs8.png 48K ostechnix-fs8.png
See? Pngquant has reduced the 152k size image to just 48k. It is indeed a good job. Open the image using any image viewer and compare them. You won't find much difference in quality between the original and compressed images. Both images will look almost same!
Display status message while compressing
By default, Pngquant will not display what's going on behind the scenes when compressing images. If you want to view the status message, use this command:
$ pngquant ostechnix.png --verbose ostechnix.png: conserving memory read 152KB file corrected image from gamma 2.2 to sRGB gamma made histogram...1854 colors found selecting colors...4% selecting colors...25% selecting colors...28% selecting colors...49% selecting colors...69% selecting colors...90% selecting colors...100% moving colormap towards local minimum eliminated opaque tRNS-chunk entries...61 entries transparent mapped image to new colors...MSE=0.055 (Q=99) writing 256-color image as ostechnix-fs8.png No errors detected while quantizing 1 image.
Now you will see the progress while compression takes place.
Compress multiple PNG images
Pngquant can also convert multiple images. The following command will compress picture1.png and picture2.png in the current working directory.
$ pngquant picture1.png picture2.png
Also, we can move all files in a directory and compress them in one go. For instance, to convert all images in a folder named "/home/sk/images", run:
$ pngquant /home/sk/images/*.png
Change output filename suffix
Like I already said, the output filename is the same as the input name except that it ends in "-fs8.png", "-or8.png". You can however change the suffix as you wish like below.
$ pngquant ostechnix.png --ext linux.png
This command will compress ostechnix.png file and save it as ostechnixlinux.png.
Skip saving files if the size of compressed files are larger than original files
What is point of compression if the converted files are larger than the source files? That conversion isn't worth, so we can skip it like below.
$ pngquant --skip-if-larger ostechnix.png
The above command will only save the converted file if it is smaller than original.
Save converted files in different path
By default, Pngquant will save the input and output image files in the same directory itself. If you want to save output file elsewhere, the command would be:
$ pngquant ostechnix.png --output Documents/output.png
This command will compress ostechnix.png and save it as output.png in Documents directory.
Compress PNG images in custom quality
Pngquant compresses the images as much as possible. We can also manually set the quality to the images. Pngquant allows us to set image quality in range 0 (worst) to 100 (perfect) using min and max parameters.
The following example sets the minimum image quality as 60 and maximum quality as 80.
$ pngquant --quality=60-80 image.png
If conversion results in quality below the minimum quality the image will not be saved.
Strip Image metadata
Image metadata is a set of data about the mages. It includes all the information about an image, such as image format, camera settings, DPI, date and time of image, title and comments, geolocation and a lot more. Refer the following article to know how to view Image metadata.
If you don't the metadata, you can simply remove it using pngquant like below.
$ pngquant --strip image.png
Fore more details, refer man pages.
$ man pngquant
Suggested read:
- Converseen - A Simple Graphical Batch Image Converter And Resizer
- 3 CLI Image Viewers To Display Images In The Terminal
- How To Display Thumbnail Images In Terminal
- How To Save Linux Command Output To An Image Or A Text File
- Gifski – A Cross-platform High-quality GIF Encoder
- Flameshot – A Simple, Yet Powerful Feature-rich Screenshot Tool
I have been using Pngquant for many years now. It greatly helps me to compress PNG images up-to 70% or more. If you're looking for PNG image optimizer and compressor, Pngquant might be a good choice.
Resources:
2 comments
Thank you, for such a nice information for Linux users.Could you tell me some graphical tool to compress PNG image size.
I am not aware of any GUI tools. Use http://www.tinpng.org site to compress PNG images online.