PNG, or Portable Network Graphics, is a raster image file format designed to replace GIF. PNG images employ lossless compression, resulting in high-quality images with a larger file size.

Some well-known PNG compression tools for Linux include optipng, pngquant, and pngng. These tools can help decrease the size of a PNG image by applying both lossy and lossless compression techniques, along with other optimizations. Among these options, pngquant is generally the preferred choice, as it appears to reduce the file size the most while maintaining high image quality.

Steps to compress PNG file in Linux:

  1. Open the terminal application.
  2. Install the 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. Determine the 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 the PNG file using pngquant.
    $ pngquant --verbose 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 the size of the resulting 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 decrease the file size and optimize pngquant usage according to your specific requirements.
    $ 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
      --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.