Losslessly Compress PNGs with optipng

Re-encode PNG files with better compression settings — pixel-identical output, typically 5-30% smaller.

LinuxpngcompressionlosslessoptipngLow risk
Run optipng -o4 image.png to losslessly optimize one file, or find . -name '*.png' -exec optipng -o2 {} + to optimize an entire directory tree.

01Command

Command
$ optipng -o4 image.png    # single file
find . -name '*.png' -exec optipng -o2 {} +   # whole tree

02What it does

optipng tries different PNG compression strategies and keeps the smallest result while guaranteeing the decoded pixels are unchanged — unlike pngquant, nothing is thrown away. -o sets the effort level from 0 to 7; -o2 is fast enough for batch runs, while -o4 to -o7 spend minutes per file for a few extra percent. Files are re-written in place, and -keep preserves a .bak backup of each original.

03Risk

RiskLow risk
Requirementsoptipng (sudo apt install optipng / sudo dnf install optipng)
!Warning: Higher optimization levels can be extremely slow on large images — use -o2 in scripts and reserve -o6/-o7 for a one-time optimization pass.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • optipng(1) — Linux man page
    Ubuntu Manpages · technical · 2026-09-17
    Evidence: optipng man page: 'Optimizes PNG files' with -o optimization levels 0-7 and -keep for backup copies.

Last verified: 2026-09-17 · Category: Media