PNG images already use DEFLATE compression (a combination of LZ77 and Huffman coding), but you can reduce file sizes further by optimizing the PNG’s internal structure and removing metadata.
OptiPNG recompresses the image data without any quality loss by trying different compression strategies and selecting the best result. Install it on Ubuntu/Debian:
sudo apt install optipng
Basic optimization:
optipng -o7 file.png
The -o flag sets optimization level (0-7). Higher levels attempt more compression trials but take longer. Level 7 provides maximum compression; level 2 is a reasonable balance for most use cases. OptiPNG modifies files in-place by default.
To strip all metadata (timestamps, color profiles, etc.) for additional size reduction:
optipng --strip all -o7 file.png
Batch processing multiple files:
optipng --strip all -o7 *.png
Run optipng -h or man optipng for full documentation.
Lossy PNG Compression with pngquant
When you can tolerate minor quality loss, pngquant significantly reduces file sizes by quantizing the color palette. It’s particularly effective for images with many colors that don’t need full 24-bit color depth.
Install on Ubuntu/Debian:
sudo apt install pngquant
Basic lossy compression:
pngquant --speed 1 file.png -o file-out.png
The --speed parameter controls compression quality and speed (1-11). Lower values produce better quality but slower compression; 1 is highest quality, 11 is fastest. For most images, speed 3-5 provides a good balance.
Preserve image quality while reducing colors:
pngquant --quality 70-95 file.png -o file-out.png
This maintains quality within the 70-95% range. The tool automatically selects the number of colors needed to stay within this range.
Batch process with quality control:
for file in *.png; do
pngquant --quality 75-95 --speed 4 "$file" -o "${file%.png}-optimized.png"
done
Force overwriting existing files:
pngquant --force --quality 75-95 --speed 4 file.png -o file.png
Comparing Results
For maximum compression, combine both approaches—use pngquant first for aggressive size reduction, then optipng for final lossless optimization:
pngquant --quality 80-95 --speed 4 input.png -o temp.png
optipng --strip all -o7 temp.png -o final.png
rm temp.png
Check file sizes at each step:
ls -lh input.png temp.png final.png
Typical results vary by image content. Photographs with subtle gradients show 30-50% reduction with pngquant alone. Screenshots and graphics with flat colors often compress 60-80% when combined with optipng.
Modern Alternatives
For new projects, consider these alternatives:
- WebP: Superior compression ratio to PNG, supported in all modern browsers. Use
cwebpfrom the libwebp package. - AVIF: Newer format with even better compression, good browser support. Requires
libavif-binor similar. - oxipng: Rust-based PNG optimizer, faster than optipng, available in most package managers.
sudo apt install oxipng
oxipng --strip safe -o max file.png
For image pipelines where both size and speed matter, oxipng offers similar quality to optipng with significantly faster execution on large batches.
