# pngquant — Compress PNG Images with Lossy Quantization

> Lossy PNG compressor that converts 24/32-bit images to 8-bit palettes – often 60–70 % smaller with barely visible quality loss.

Source: https://www.jpkc.com/db/en/cheatsheets/images-media/pngquant/

<!-- PROSE:intro -->
pngquant shrinks PNG files dramatically by reducing 24/32-bit images to an 8-bit palette with alpha transparency – often 60–70 % smaller with barely any visible difference. The quantization is lossy, though: by default it writes a new `name-fs8.png` file, but `--ext .png --force` overwrites the original in place, so keep a backup. Where genuine lossless output is required, reach for optipng instead.
<!-- PROSE:intro:end -->

## Basic Usage

`pngquant <image.png>` — Quantize to 256 colors, output as <image>-fs8.png.

```bash
pngquant screenshot.png
```

`pngquant <n> <image.png>` — Quantize to a specific number of colors (2-256).

```bash
pngquant 128 screenshot.png
```

`pngquant --output <output.png> <input.png>` — Write to a specific output file.

```bash
pngquant --output optimized.png original.png
```

`pngquant -o <output.png> <input.png>` — Short form of --output.

```bash
pngquant -o small.png large.png
```

`pngquant --ext .png --force <image.png>` — Overwrite the original file in-place.

```bash
pngquant --ext .png --force screenshot.png
```

`pngquant *.png` — Batch quantize all PNGs (creates *-fs8.png files).

```bash
pngquant images/*.png
```

## Quality Control

`pngquant --quality=<min>-<max> <image.png>` — Set quality range (0-100). Aborts with exit code 99 if min can't be achieved.

```bash
pngquant --quality=65-80 photo.png
```

`pngquant --quality=80-100 <image.png>` — High quality (subtle compression, smaller savings).

```bash
pngquant --quality=80-100 icon.png
```

`pngquant --quality=45-65 <image.png>` — Aggressive compression (larger savings, visible on close inspection).

```bash
pngquant --quality=45-65 background.png
```

`pngquant --quality=0-100 <image.png>` — Accept any quality (never aborts, maximum compression).

```bash
pngquant --quality=0-100 image.png
```

`pngquant --skip-if-larger <image.png>` — Don't write output if it would be larger than the original.

```bash
pngquant --skip-if-larger icon.png
```

## Color & Dithering

`pngquant 64 <image.png>` — Reduce to 64 colors (good for simple graphics).

```bash
pngquant 64 icon.png
```

`pngquant 16 <image.png>` — Reduce to 16 colors (very small, best for icons/logos).

```bash
pngquant 16 logo.png
```

`pngquant --nofs <image.png>` — Disable Floyd-Steinberg dithering (sharper edges, potential banding).

```bash
pngquant --nofs icon.png
```

`pngquant --floyd=<n> <image.png>` — Set dithering level (0.0 = none, 1.0 = full, default: 1.0).

```bash
pngquant --floyd=0.5 photo.png
```

`pngquant --posterize <bits> <image.png>` — Reduce precision of color channels (useful for gradients).

```bash
pngquant --posterize 4 gradient.png
```

## Speed & Performance

`pngquant --speed <n> <image.png>` — Set speed/quality trade-off (1 = best quality, 11 = fastest).

```bash
pngquant --speed 1 photo.png
```

`pngquant --speed 1 --quality=65-80 <image.png>` — Slowest speed for best quality result.

```bash
pngquant --speed 1 --quality=65-80 hero-image.png
```

`pngquant --speed 11 *.png` — Fastest processing for batch operations.

```bash
pngquant --speed 11 thumbnails/*.png
```

## Output Options

`pngquant --ext <suffix> <image.png>` — Set output file suffix (default: -fs8.png).

```bash
pngquant --ext -small.png screenshot.png
```

`pngquant --strip <image.png>` — Strip all metadata from output.

```bash
pngquant --strip photo.png
```

`pngquant - < <input.png> > <output.png>` — Read from stdin, write to stdout (for piping).

```bash
cat input.png | pngquant - > output.png
```

`pngquant --verbose <image.png>` — Show detailed processing info (remapped colors, file sizes).

```bash
pngquant --verbose screenshot.png
```

## Common Patterns

`pngquant --quality=65-80 --speed 1 --strip --ext .png --force *.png` — Web optimization: in-place, stripped, quality-controlled.

```bash
pngquant --quality=65-80 --speed 1 --strip --ext .png --force assets/*.png
```

`find . -name '*.png' -exec pngquant --quality=65-80 --skip-if-larger --strip --ext .png --force {} \;` — Recursively optimize all PNGs in-place (safe, skips if larger).

```bash
find public/ -name '*.png' -exec pngquant --quality=65-80 --skip-if-larger --strip --ext .png --force {} \;
```

`pngquant --quality=65-80 --strip photo.png && optipng -o2 photo-fs8.png` — Pipeline: pngquant (lossy) then optipng (lossless) for maximum compression.

```bash
pngquant --quality=65-80 --strip photo.png && optipng -o2 photo-fs8.png
```

`pngquant 256 --quality=80-100 --skip-if-larger --nofs logo.png` — Optimize a logo/icon (no dithering for sharp edges, high quality).

```bash
pngquant 256 --quality=80-100 --skip-if-larger --nofs logo.png
```

`for f in *.png; do pngquant --quality=65-80 --strip -o "dist/$f" "$f"; done` — Batch optimize into a separate directory.

```bash
mkdir -p dist && for f in src/*.png; do pngquant --quality=65-80 --strip -o "dist/$(basename $f)" "$f"; done
```

<!-- PROSE:outro -->
## Conclusion

pngquant is one of the most effective tools for lean web PNGs: with `--quality 65-80`, `--speed` and a sensible colour count you get large savings the eye won't notice. Keep in mind that the compression is lossy – once colours are quantized, they're gone for good. The default protects you by writing a fresh `-fs8.png`; the moment you combine `--force` with `--ext .png` (or `--output`), you overwrite the original irreversibly, so work from backups only. If the result falls below your quality minimum, pngquant aborts with exit code 99 and leaves the file untouched. For strictly lossless requirements, optipng is the right choice.

## Further Reading

- [pngquant – official website](https://pngquant.org/) – documentation, options and downloads for the quantizer
- [pngquant on GitHub](https://github.com/kornelski/pngquant) – source code, issues and the underlying libimagequant library
<!-- PROSE:outro:end -->

## Related Commands

- [optipng](https://www.jpkc.com/db/en/cheatsheets/images-media/optipng/) – lossless PNG optimizer that pairs perfectly with pngquant
- [gifsicle](https://www.jpkc.com/db/en/cheatsheets/images-media/gifsicle/) – create and optimize animated GIFs
- [convert](https://www.jpkc.com/db/en/cheatsheets/images-media/convert/) – convert and edit images with ImageMagick

