PNG screenshots, interface mockups, and diagrams often move through tickets, repositories, and web deployments where avoidable bytes slow reviews or trigger upload limits. Compressing the file in place too early can hide color loss, so start with a separate reduced copy and compare it before replacing the original.
pngquant reduces a true-color PNG by writing a palette-based PNG that can still keep transparency. The --output option names a review copy, --strip removes optional metadata chunks, and --skip-if-larger avoids saving a result that does not reduce the file enough to justify the conversion.
The conversion is lossy because fewer colors are stored. Screenshots, diagrams, and flat interface assets usually tolerate this better than photos, gradients, or artwork with subtle shading, so visual review remains part of the workflow before any command overwrites the source image.
Steps to compress a PNG file with pngquant in Linux:
- Open the directory that contains the PNG file.
$ cd ~/images
Without --output or --ext, pngquant writes its converted file beside the source image with a suffix such as -fs8.png or -or8.png.
- Check the source file size.
$ ls -lh sample.png -rw-r--r-- 1 user user 2.0M Jun 13 2026 sample.png
- Write a compressed review copy.
$ pngquant --quality=65-85 --skip-if-larger --strip --output sample-compressed.png sample.png
--quality=65-85 keeps the result above the minimum quality score, --strip removes optional PNG metadata, and --output leaves the source file untouched.
- Compare the source and compressed file sizes.
$ ls -lh sample.png sample-compressed.png -rw-r--r-- 1 user user 15K Jun 13 2026 sample-compressed.png -rw-r--r-- 1 user user 2.0M Jun 13 2026 sample.png
If pngquant exits with status 98, the converted file was not worth saving. Status 99 means the requested minimum quality could not be met.
- Confirm that the compressed output is still a PNG file.
$ file sample-compressed.png sample-compressed.png: PNG image data, 960 x 540, 4-bit colormap, non-interlaced
A colormap result is expected when pngquant reduces the image to a smaller palette.
- Open the compressed copy and inspect edges, gradients, and transparent areas.
Do not replace the source image when banding, halos, text fuzziness, or transparency changes are visible in the compressed copy.
- Replace the source file only after the compressed copy looks acceptable.
$ pngquant --quality=65-85 --skip-if-larger --strip --ext .png --force sample.png
--ext .png with --force writes back to the original path.
- Verify the final file size.
$ ls -lh sample.png -rw-r--r-- 1 user user 15K Jun 13 2026 sample.png
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.