PNG or Portable Network Graphics is a raster image file format meant to replace GIF. PNG images use lossless image compression, producing high-quality images at a higher cost for file size.

Some popular PNG compressors for Linux are optipng, pngquant, and pngng. These tools can help reduce the size of a PNG image by performing lossy and lossless compression along with other optimizations. pngquant is usually the best option as it seems to optimize the file size the most without sacrificing much quality.

Steps to compress PNG file 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 #Ubuntu and variants
    > sudo zypper refresh && sudo zypper install --non-interactive pngquant # SUSE and variants
    $ sudo dnf install --assumeyes pngquant # Red Hat and variants
  3. Check current size of your PNG image file.
    $ ls -lh filename.png 
    -rwxr--r-- 1 user user 295K Oct 11 09:40 filename.png
  4. Compress PNG file using pngquant.
    $ pngquant --verbose filename.png 
    filename.png:
      read 295KB file
      made histogram...3507 colors found
      selecting colors...5%
      selecting colors...35%
      selecting colors...70%
      selecting colors...100%
      moving colormap towards local minimum
      eliminated opaque tRNS-chunk entries...6 entries transparent
      mapped image to new colors...MSE=0.064 (Q=99)
      writing 256-color image as filename-fs8.png
      copied 1KB of additional PNG metadata
    Quantized 1 image.
  5. Check size of generated file to compare.
    $ ls -lh filename*.png 
    -rw-rw-r-- 1 user user  73K Oct 11 09:42 filename-fs8.png
    -rwxr--r-- 1 user user 295K Oct 11 09:40 filename.png

    -fs8 is appended to the compressed filename by default.

  6. Further reduce file size and optimize pngquant usage based on your specific needs.
    $ pngquant --help
    pngquant, 2.12.2 (July 2019), by Kornel Lesinski, Greg Roelofs.
       Compiled with no support for color profiles. Using libpng 1.6.37.
    
    usage:  pngquant [options] [ncolors] -- pngfile [pngfile ...]
            pngquant [options] [ncolors] - >stdout <stdin
    
    options:
      --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, 4=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.
Discuss the article:

Comment anonymously. Login not required.