optipng — Optimize PNG Files Losslessly

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

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.

Basic Optimization

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

optipng screenshot.png

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

optipng -o7 screenshot.png

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

optipng -o2 images/*.png

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

optipng -out optimized.png original.png

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

optipng -dir optimized/ *.png

Optimization Levels

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

optipng -o0 screenshot.png

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

optipng -o1 screenshot.png

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

optipng -o2 screenshot.png

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

optipng -o3 screenshot.png

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

optipng -o5 screenshot.png

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

optipng -o7 icon.png

Metadata & Backups

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

optipng -strip all screenshot.png

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

optipng -keep screenshot.png

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

optipng -preserve screenshot.png

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

optipng -clobber -out optimized.png screenshot.png

Format Conversion

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

optipng screenshot.bmp

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

optipng icon.gif

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

optipng scan.tiff

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

optipng -out result.png image.ppm

Advanced Options

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

optipng -simulate screenshot.png

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

optipng -v screenshot.png

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

optipng -i 1 photo.png

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

optipng -i 0 screenshot.png

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

optipng -nb screenshot.png

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

optipng -nc photo.png

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

optipng -np icon.png

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

optipng -nx screenshot.png

Common Patterns

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

optipng -o2 -strip all assets/*.png

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

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

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

optipng -o7 -strip all -preserve favicon.png

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

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.

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

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

  • pngquant – lossy PNG quantization for even smaller files
  • jpegtran – lossless optimization and transformation of JPEG files
  • convert – convert and edit images with ImageMagick