PNG or Portable Network Graphics is a raster image file format that is meant to replace GIF. PNG utilise lossless image compression which results in high quality image though sometimes can be relatively big in size.

Tools such as optipng, pngquant and pngng can further reduce the size of a PNG image in Linux by performing lossy and lossless compression. pngquant is normally the best option as it seem to be able to optimize the file size the most without sacrificing much on quality.

Steps to compress PNG image and reduce file size in Linux:

  1. Launch terminal application.
  2. Install pngquant package for your system.
    $ sudo apt update && sudo apt install --assume-yes pngquant
    [sudo] password for user:
    Hit:1 disco InRelease
    The following NEW packages will be installed:
      libimagequant0 pngquant
    0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
    Need to get 50.6 kB of archives.
    After this operation, 141 kB of additional disk space will be used.
    ##### snipped
  3. Check current file size.
    $ ls -lh filename.png
    -rw-r--r-- 1 user user 401K Jul 18 08:32 filename.png
  4. Compress PNG file using pngquant.
    $ pngquant filename.png

    Further options for pngquant:

    $ pngquant --help
    pngquant, 2.12.0 (January 2018), by Kornel Lesinski, Greg Roelofs.
       Compiled with no support for color profiles. Using libpng 1.6.36.
    usage:  pngquant [options] [ncolors] -- pngfile [pngfile ...]
            pngquant [options] [ncolors] - >stdout <stdin
      --force           overwrite existing output files (synonym: -f)
      --skip-if-larger  only save converted files if they're smaller than original
      --output file     destination file path to use instead of --ext (synonym: -o)
      --ext new.png     set custom suffix/extension for output filenames
      --quality min-max don't save below min, use fewer colors below max (0-100)
      --speed N         speed/quality trade-off. 1=slow, 3=default, 11=fast & rough
      --nofs            disable Floyd-Steinberg dithering
      --posterize N     output lower-precision color (e.g. for ARGB4444 output)
      --strip           remove optional metadata (default on Mac)
      --verbose         print status messages (synonym: -v)
    Quantizes one or more 32-bit RGBA PNGs to 8-bit (or smaller) RGBA-palette.
    The output filename is the same as the input name except that
    it ends in "-fs8.png", "-or8.png" or your custom extension (unless the
    input is stdin, in which case the quantized image will go to stdout).
    If you pass the special output path "-" and a single input file, that file
    will be processed and the quantized image will go to stdout.
    The default behavior if the output file exists is to skip the conversion;
    use --force to overwrite. See man page for full list of options.
  5. Check size of generated file to compare.
    $ ls -lh filename*.png
    -rw-rw-r-- 1 user user 104K Jul 18 08:35 filename-fs8.png
    -rw-r--r-- 1 user user 401K Jul 18 08:32 filename.png

    -fs8 is appended to the compressed filename by default.

Share this guide!
Discuss the article:

Comment anonymously. Login not required.