Losslessly Compress PNG Images via Linux Command Line

PNG files are losslessly compressed images. It’s a great format to use when you’re concerned about retaining all the quality of your image. Otherwise, opting for the JPG format can yield a better file size at the cost of some quality. PNG files take up quite a bit more storage space, but we can use the optipng tool on Linux to optimize the compression of our PNG files, resulting in a smaller file size and zero quality loss.

After running optipng on a PNG file, the resulting image data is bit-for-bit the exact same as the original, but the file can enjoy a significant reduction in size.

There are a lot of programs and websites that claim to reduce image file size for you, but be careful. Some of these tools will indeed reduce the size of your images, but they work by recompressing the photo, resulting in a marginal (and sometimes quite noticeable) loss of quality. You may not always notice the dip in quality, but it’s there.

In this guide, we’ll show you how to install the optipng tool on Linux, and go over some example commands to help you learn how to use it on your own pictures.

Is optipng Really Lossless?

Our photos are valuable, and we wouldn’t want them to lose quality that we can never get back. To verify that optipng isn’t touching the photo data itself, we tried optimizing a photo and then used graphicsmagick to compare it to the original.

$ cp mountain.png mountain-optim.png
$ optipng mountain-optim.png
$ gm compare -metric mse mountain.png mountain-optim.png
Image Difference (MeanSquaredError):
           Normalized    Absolute
          ============  ==========
     Red: 0.0000000000        0.0
   Green: 0.0000000000        0.0
    Blue: 0.0000000000        0.0
   Total: 0.0000000000        0.0
$ du -h mountain*
38M     mountain-optim.png
46M     mountain.png

As you can see, our PNG image was compressed about 8MB (7.85% decrease), and the optimized image is identical to the original. Even the photo’s metadata is left intact.

Install optipng on Linux

Install the optipng tool with your system’s package manager by using the appropriate command below.

Ubuntu, Debian, and Linux Mint:

$ sudo apt install optipng

Fedora, AlmaLinux, CentOS, and RHEL:

$ sudo dnf install epel-release
$ sudo dnf install optipng

Arch Linux and Manjaro:

$ sudo pacman -S optipng

optipng Command Examples

Now that optipng is installed, check out some of the command examples below to learn how to use it.

1. To optimize an image in-place, simply pass the name of the PNG file to optipng. Of course, you can also use wildcards or specify multiple files in your command, which works well if you have a whole directory of images to optimize.

$ optipng image.png
** Processing: image.png
9006x3924 pixels, 3x8 bits/pixel, RGB
Input IDAT size = 47896899 bytes
Input file size = 47902044 bytes

  zc = 9  zm = 8  zs = 0  f = 5         IDAT size = 43231615

Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 5         IDAT size = 43231615

Output IDAT size = 43231615 bytes (4665284 bytes decrease)
Output file size = 43236220 bytes (4665824 bytes = 9.74% decrease)

2. Unless you’re in a hurry, you should always supply the -o7 option with your command. This will set the optimization level as high as possible. optipng will try different compression techniques, and select whichever one works the best for the file. It might take a little longer for optipng to process the file, but the trade off is worth it.

$ optipng -o7 image.png

3. If you don’t want optipng to overwrite the original files, use the -keep (or just -k) option to keep a backup of the modified files.

$ optipng -o7 -k *.png

4. To reduce size even further, you can pass the --strip all option, which will clear metadata from the file, including Exif data like the date it was taken, with what device, GPS location, etc. Be careful, because sometimes this information is nice to have. But if you’re sharing the picture online, metadata can be a privacy concern, and smaller file sizes are usually preferred.

$ optipng -o7 --strip all image.png

5. To execute optipng on all images recursively, we can use the find command. The following command will optimize all *.png (and *.PNG) files in your current directory, as well as all of those in any subdirectories. Note that we’ve also included the -o7 --strip all options in this command.

$ find . -type f \( -iname "*.png" \) -exec optipng -o7 --strip all {} \;

That should be all you need to know to get the most out of optipng. There’s quite a bit of documentation in the man page if you want to read up on some of its other options, or learn the technical process of how optipng accomplishes all this optimization.

$ man optipng

You may also be interested in how to use jpegoptim, a similar command line tool that can optimize JPG files.

Leave a Comment

Your email address will not be published. Required fields are marked *