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.pngoptipng -o<n> <image.png> — Set optimization level (0-7, higher = slower but smaller).
optipng -o7 screenshot.pngoptipng -o2 *.png — Batch optimize all PNGs in the current directory.
optipng -o2 images/*.pngoptipng -out <output.png> <input.png> — Write to a different file (keep original unchanged).
optipng -out optimized.png original.pngoptipng -dir <directory> <image.png> — Write optimized files to a separate directory.
optipng -dir optimized/ *.pngOptimization Levels
optipng -o0 <image.png> — Level 0: no compression trials, just a pass-through (fastest).
optipng -o0 screenshot.pngoptipng -o1 <image.png> — Level 1: single compression trial (fast).
optipng -o1 screenshot.pngoptipng -o2 <image.png> — Level 2: 8 compression trials (default, good balance).
optipng -o2 screenshot.pngoptipng -o3 <image.png> — Level 3: 16 trials (slower, better compression).
optipng -o3 screenshot.pngoptipng -o5 <image.png> — Level 5: 48 trials (slow, near-optimal for most images).
optipng -o5 screenshot.pngoptipng -o7 <image.png> — Level 7: 240 trials (very slow, maximum compression).
optipng -o7 icon.pngMetadata & Backups
optipng -strip all <image.png> — Strip all metadata chunks (text, time, ICC profiles, etc.).
optipng -strip all screenshot.pngoptipng -keep <image.png> — Keep a backup (.bak) of the original before overwriting.
optipng -keep screenshot.pngoptipng -preserve <image.png> — Preserve file attributes (timestamps, permissions) after optimization.
optipng -preserve screenshot.pngoptipng -clobber -out <output.png> <image.png> — Overwrite an already existing output file.
optipng -clobber -out optimized.png screenshot.pngFormat Conversion
optipng <image.bmp> — Convert BMP to optimized PNG (writes a .png file, keeps the BMP).
optipng screenshot.bmpoptipng <image.gif> — Convert GIF to optimized PNG (static GIFs only).
optipng icon.gifoptipng <image.tiff> — Convert TIFF to optimized PNG.
optipng scan.tiffoptipng -out <output.png> <image.pnm> — Convert PNM (PBM/PGM/PPM) to optimized PNG.
optipng -out result.png image.ppmAdvanced Options
optipng -simulate <image.png> — Simulate optimization (show savings without modifying the file).
optipng -simulate screenshot.pngoptipng -v <image.png> — Verbose output (show compression trials and results).
optipng -v screenshot.pngoptipng -i 1 <image.png> — Enable Adam7 interlacing (progressive loading).
optipng -i 1 photo.pngoptipng -i 0 <image.png> — Force non-interlaced output.
optipng -i 0 screenshot.pngoptipng -nb <image.png> — Skip bit depth reduction trials.
optipng -nb screenshot.pngoptipng -nc <image.png> — Skip color type reduction trials.
optipng -nc photo.pngoptipng -np <image.png> — Skip palette reduction trials.
optipng -np icon.pngoptipng -nx <image.png> — Skip all reductions (only re-compress with optimal ZLIB parameters).
optipng -nx screenshot.pngCommon Patterns
optipng -o2 -strip all *.png — Optimize and strip metadata from all PNGs (best for web deployment).
optipng -o2 -strip all assets/*.pngfind . -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.pngfind . -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
- optipng — Official project page – documentation, manual and downloads of the original project
- Wikipedia: OptiPNG – background on the lossless PNG optimizer