# optipng — Optimize PNG Files Losslessly

> Practical guide to optipng: losslessly shrink PNG files – optimization levels, metadata stripping and safe batch runs.

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

<!-- PROSE:intro -->
optipng shrinks PNG files **losslessly** – with no quality loss at all: it tries different filter and compression parameters and keeps only the smallest result. By default it works **in-place** and overwrites the original, so a backup belongs before every batch run (or use `-keep`, `-out`, `-dir`). If you need more aggressive savings, reach for pngquant (lossy) or zopflipng (stronger but slower). This guide walks you through the commands you actually reach for daily.
<!-- PROSE:intro:end -->

## Basic Optimization

`optipng <image.png>` — Optimize a PNG file in-place (default level: -o2).

```bash
optipng screenshot.png
```

`optipng -o<n> <image.png>` — Set optimization level (0-7, higher = slower but smaller).

```bash
optipng -o7 screenshot.png
```

`optipng -o2 *.png` — Batch optimize all PNGs in the current directory.

```bash
optipng -o2 images/*.png
```

`optipng -out <output.png> <input.png>` — Write to a different file (keep original unchanged).

```bash
optipng -out optimized.png original.png
```

`optipng -dir <directory> <image.png>` — Write optimized files to a separate directory.

```bash
optipng -dir optimized/ *.png
```

## Optimization Levels

`optipng -o0 <image.png>` — Level 0: no compression trials, just a pass-through (fastest).

```bash
optipng -o0 screenshot.png
```

`optipng -o1 <image.png>` — Level 1: single compression trial (fast).

```bash
optipng -o1 screenshot.png
```

`optipng -o2 <image.png>` — Level 2: 8 compression trials (default, good balance).

```bash
optipng -o2 screenshot.png
```

`optipng -o3 <image.png>` — Level 3: 16 trials (slower, better compression).

```bash
optipng -o3 screenshot.png
```

`optipng -o5 <image.png>` — Level 5: 48 trials (slow, near-optimal for most images).

```bash
optipng -o5 screenshot.png
```

`optipng -o7 <image.png>` — Level 7: 240 trials (very slow, maximum compression).

```bash
optipng -o7 icon.png
```

## Metadata & Backups

`optipng -strip all <image.png>` — Strip all metadata chunks (text, time, ICC profiles, etc.).

```bash
optipng -strip all screenshot.png
```

`optipng -keep <image.png>` — Keep a backup (.bak) of the original before overwriting.

```bash
optipng -keep screenshot.png
```

`optipng -preserve <image.png>` — Preserve file attributes (timestamps, permissions) after optimization.

```bash
optipng -preserve screenshot.png
```

`optipng -clobber -out <output.png> <image.png>` — Overwrite an already existing output file.

```bash
optipng -clobber -out optimized.png screenshot.png
```

## Format Conversion

`optipng <image.bmp>` — Convert BMP to optimized PNG (writes a .png file, keeps the BMP).

```bash
optipng screenshot.bmp
```

`optipng <image.gif>` — Convert GIF to optimized PNG (static GIFs only).

```bash
optipng icon.gif
```

`optipng <image.tiff>` — Convert TIFF to optimized PNG.

```bash
optipng scan.tiff
```

`optipng -out <output.png> <image.pnm>` — Convert PNM (PBM/PGM/PPM) to optimized PNG.

```bash
optipng -out result.png image.ppm
```

## Advanced Options

`optipng -simulate <image.png>` — Simulate optimization (show savings without modifying the file).

```bash
optipng -simulate screenshot.png
```

`optipng -v <image.png>` — Verbose output (show compression trials and results).

```bash
optipng -v screenshot.png
```

`optipng -i 1 <image.png>` — Enable Adam7 interlacing (progressive loading).

```bash
optipng -i 1 photo.png
```

`optipng -i 0 <image.png>` — Force non-interlaced output.

```bash
optipng -i 0 screenshot.png
```

`optipng -nb <image.png>` — Skip bit depth reduction trials.

```bash
optipng -nb screenshot.png
```

`optipng -nc <image.png>` — Skip color type reduction trials.

```bash
optipng -nc photo.png
```

`optipng -np <image.png>` — Skip palette reduction trials.

```bash
optipng -np icon.png
```

`optipng -nx <image.png>` — Skip all reductions (only re-compress with optimal ZLIB parameters).

```bash
optipng -nx screenshot.png
```

## Common Patterns

`optipng -o2 -strip all *.png` — Optimize and strip metadata from all PNGs (best for web deployment).

```bash
optipng -o2 -strip all assets/*.png
```

`find . -name '*.png' -exec optipng -o2 -strip all {} \;` — Recursively optimize all PNGs in subdirectories.

```bash
find public/ -name '*.png' -exec optipng -o2 -strip all {} \;
```

`optipng -o7 -strip all -preserve icon.png` — Maximum optimization for a single important asset.

```bash
optipng -o7 -strip all -preserve favicon.png
```

`find . -name '*.png' -exec optipng -simulate {} \; 2>&1 | grep 'decrease'` — Preview which PNGs can be further optimized.

```bash
find . -name '*.png' -exec optipng -simulate {} \; 2>&1 | grep 'decrease'
```

`optipng -o2 -strip all -dir dist/ src/*.png` — Optimize source PNGs into a distribution directory.

```bash
optipng -o2 -strip all -dir dist/images/ src/images/*.png
```

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

optipng is the go-to tool when PNGs need to get smaller without any quality loss: `-o2` is plenty for everyday work, while `-o7` squeezes out the last bytes for individual important assets. Keep the crucial point in mind: optipng writes **in-place** and overwrites the original – back up your files before batch runs, or use `-keep` (which creates a `.bak`), `-out` or `-dir`. If you need even smaller files and can accept minimal color loss, pngquant adds lossy quantization; zopflipng compresses losslessly a touch harder still, but is markedly slower.

## Further Reading

- [optipng — Official project page](https://optipng.sourceforge.net/) – documentation, manual and downloads of the original project
- [Wikipedia: OptiPNG](https://en.wikipedia.org/wiki/OptiPNG) – background on the lossless PNG optimizer
<!-- PROSE:outro:end -->

## Related Commands

- [pngquant](https://www.jpkc.com/db/en/cheatsheets/images-media/pngquant/) – lossy PNG quantization for even smaller files
- [jpegtran](https://www.jpkc.com/db/en/cheatsheets/images-media/jpegtran/) – lossless optimization and transformation of JPEG files
- [convert](https://www.jpkc.com/db/en/cheatsheets/images-media/convert/) – convert and edit images with ImageMagick

