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 larger file sizes. This makes PNG ideal for retaining image details but can create challenges when file size is a concern, particularly for web usage or limited storage environments.

Several well-known PNG compression tools are available for Linux, including optipng, pngquant, and pngcrush. 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 because it effectively reduces file size while maintaining high image quality.

Using pngquant, users can compress PNG files in Linux with a straightforward process. This involves installing the tool, running the compression, and checking the resulting file size. Below is a step-by-step guide on how to accomplish this.

Steps to compress PNG images in Linux:

  1. Open the terminal on your Linux system.
  2. Install the pngquant tool using your package manager.
    $ 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

    The installation command may vary depending on your Linux distribution.

  3. Check 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 
    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.

    By default, pngquant appends “-fs8” to the compressed filename.

  5. Check the size of the compressed 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
  6. Optimize further as needed by adjusting pngquant options.
    $ 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.

    Use options like --quality and --speed to fine-tune compression.

Discuss the article:

Comment anonymously. Login not required.