Efficient compression of PNG images on Linux reduces bandwidth usage, shortens upload times, and saves storage while preserving transparency and sharp edges. Smaller image assets improve page load performance for websites, documentation, and web applications without forcing a move to lossy formats such as JPEG.
The PNG format uses lossless DEFLATE compression and stores full-color images with an optional alpha channel. The pngquant utility reduces file size by converting 24/32‑bit RGBA images to 8‑bit palette PNGs using perceptual quantization, typically cutting file size by 50–70% while keeping most images visually indistinguishable from the original, especially icons, UI elements, and diagrams.
Because the quantization step is lossy, backups or version control are important before replacing originals in production assets. By default, pngquant writes a new file with the ‑fs8 suffix instead of modifying the source image, which simplifies side‑by‑side comparison. Commands rely on a working terminal environment on Linux and expect basic familiarity with navigating directories and inspecting files.
Steps to compress PNG images in Linux:
- Open a terminal on a Linux system.
- Install the pngquant tool if it is not already available using the distribution package manager.
$ sudo apt update && sudo apt install --assume-yes pngquant
On Fedora, CentOS Stream, and Red Hat Enterprise Linux, a similar result is obtained with sudo dnf install --assumeyes pngquant; on openSUSE, use sudo zypper install --no-confirm pngquant.
- Change into the directory containing the target PNG file.
$ cd /path/to/images
- Inspect the current size of the original PNG image using ls -lh.
$ ls -lh sample.png -rw-r--r-- 1 root root 172K Feb 6 2024 sample.png
Checking the original size provides a baseline for evaluating the compression gain.
- Run pngquant in verbose mode on the original file to create a compressed output.
$ pngquant --verbose sample.png sample.png: read 172KB file made histogram...5234 colors found selecting colors...6% selecting colors...46% selecting colors...93% selecting colors...100% moving colormap towards local minimum eliminated opaque tRNS-chunk entries...0 entries transparent mapped image to new colors...MSE=1.311 (Q=95) writing 256-color image as sample-fs8.png copied 1KB of additional PNG metadata Quantized 1 image.
By default, pngquant writes a new file named with the ‑fs8 suffix so the original image remains unchanged.
- Compare the original and compressed file sizes using a glob pattern with ls -lh.
$ ls -lh sample*.png -rw-r--r-- 1 root root 51K Dec 28 11:11 sample-fs8.png -rw-r--r-- 1 root root 172K Feb 6 2024 sample.png
The size reduction indicates how effective the quantization was for the specific image.
- Tune compression by rerunning pngquant with --quality and --speed options according to visual requirements.
$ pngquant --quality 60-80 --speed 1 --output sample-quality.png sample.png
Lower --quality values and slower --speed settings typically improve visual fidelity at the cost of processing time.
- Display help output when deeper tuning or batch processing options are needed.
$ pngquant --help pngquant, 2.18.0 (January 2023), by Kornel Lesinski, Greg Roelofs. Compiled with no support for color profiles. Using libpng 1.6.43. usage: pngquant [options] [ncolors] -- pngfile [pngfile ...] pngquant [options] [ncolors] - >stdout <stdin options: --force overwrite existing output files (synonym: -f) --skip-if-larger only save converted files if they're smaller than original --output file destination file path to use instead of --ext (synonym: -o) --ext new.png set custom suffix/extension for output filenames --quality min-max don't save below min, use fewer colors below max (0-100) --speed N speed/quality trade-off. 1=slow, 4=default, 11=fast & rough ##### snipped #####Options such as --skip-if-larger and --ext are useful when integrating pngquant into build pipelines or scripts.
- Open the compressed image in an image viewer to confirm that visual quality meets expectations before replacing the original asset.
Replacing original assets without visually checking the compressed output can introduce visible banding or color shifts in gradients, logos, or brand colors.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
