Compress PNG image files easily in Linux

Guten Tag!

In this post, we will learn to Compress PNG image files easily in Linux.

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.

There are many popular PNG image compressors for Linux, like optipng, pngquant, and pngng. And in this post, I want to try pngquant.

Step 1: Install PNGQuant

Install PNGQuant software with run the following command in terminal session:

$ 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

Step 2: Check the current size of your image

Check the current size of your PNG image file that you want to compress with:

ls -lh filename.png 

I'm going to use a sample png that downloaded from https://www.sample-videos.com/download-sample-png-image.php

root@ariq01:~# wget https://www.sample-videos.com/img/Sample-png-image-200kb.png --no-check-certificate
--2023-02-17 15:44:18--  https://www.sample-videos.com/img/Sample-png-image-200kb.png
Resolving www.sample-videos.com (www.sample-videos.com)... 103.145.51.95
Connecting to www.sample-videos.com (www.sample-videos.com)|103.145.51.95|:443... connected.
WARNING: no certificate subject alternative name matches
        requested host name ‘www.sample-videos.com’.
HTTP request sent, awaiting response... 200 OK
Length: 207071 (202K) [image/png]
Saving to: ‘Sample-png-image-200kb.png’

Sample-png-image-20 100%[===================>] 202.22K   203KB/s    in 1.0s

2023-02-17 15:44:20 (203 KB/s) - ‘Sample-png-image-200kb.png’ saved [207071/207071]

Step 3: Compress PNG file using pngquant.

Compress the png image file with this command:

pngquant --verbose filename.png 

Output:

root@ariq01:~# pngquant --verbose Sample-png-image-200kb.png
Sample-png-image-200kb.png:
  read 203KB file
  converted image from gamma 2.2 to gamma 2.2
  made histogram...59148 colors found
  selecting colors...11%
  selecting colors...77%
  selecting colors...100%
  moving colormap towards local minimum
  eliminated opaque tRNS-chunk entries...0 entries transparent
  mapped image to new colors...MSE=5.396 (Q=82)
  writing 256-color image as Sample-png-image-200kb-fs8.png
  copied 1KB of additional PNG metadata
Quantized 1 image.

Let's take a look at a newer size of Sample-png-image-200kb.png file to compare

root@ariq01:~# ls -lah Sample-png-image-200kb*
-rw-r--r-- 1 root root  48K Feb 17 15:46 Sample-png-image-200kb-fs8.png
-rw-r--r-- 1 root root 203K Oct 31  2020 Sample-png-image-200kb.png

The result of the compressed one is Sample-png-image-200kb-fs8.png, compressed to 48Kb from 203Kb.

Read more about pngquant here:

GitHub - kornelski/pngquant: Lossy PNG compressor — pngquant command based on libimagequant library
Lossy PNG compressor — pngquant command based on libimagequant library - GitHub - kornelski/pngquant: Lossy PNG compressor — pngquant command based on libimagequant library

Optional: Compare with imagecompressor.com

Sometimes to speed up time, I use imagecompressor.com rather than using the Linux one. The user interface is quite simple, you can upload images by drag-down to the media uploader area or using the Upload files button.

And the result is same:

That's great.. or maybe imagecompressor.com uses pngquant as the back-end? I don't know haha

Thanks for reading!

Verabschiedung