Linux / Unix Command To Optimize and Compress PNG Files In Bulk 2/2

Categories:  Linux
Labels:  internet, bash script, optimization

Use ls command to see file size:

$ ls -lh test.png 

Sample outputs:

-rw-r--r-- 1 vivek vivek 279K Nov 29 00:10 test.png

Use optipng optimization program as follows:

$ cp test.{png,bak}
$ optipng test.png

Sample outputs:

OptiPNG 0.6.4: Advanced PNG optimizer.
Copyright (C) 2001-2010 Cosmin Truta.
 
** Processing: test.png
1164x911 pixels, 3x8 bits/pixel, RGB
Input IDAT size = 284778 bytes
Input file size = 285286 bytes
 
Trying:
  zc = 9  zm = 8  zs = 0  f = 0		IDAT size = 215274
 
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0		IDAT size = 215274
 
Output IDAT size = 215274 bytes (69504 bytes decrease)
Output file size = 215374 bytes (69912 bytes = 24.51% decrease)

File size is reduced by 24.51%:

$ ls -lh test.png

Sample outputs:

-rw-r--r-- 1 vivek vivek 211K Nov 29 02:40 test.png

You can verify the quality of both images.

Optimized test.png file ensures that it loads faster for users on slow connections. It will also save 24.51% bytes per image view in terms of both network bandwidth and storage. According to Google:

You should see a benefit for any image file that can be reduced by 25 bytes or more (less than this will not result in any appreciable performance gain).

How do I optimize files in bulk?

Use a bash for loop as follows:

1
2
3
4
5
6
#!/bin/bash
cd /path/to/png/storage/2010/01/c/
mkdir optimized
## store optimized images in optimized directory ##
## Keep file system permission and make a backup of original PNG (see options below)  ##
for i in *.png; do optipng -o5 -quiet -keep -preserve -dir optimized -log optipng.log "$i"; done

You need to make changes to your code. For example, url path to optimize url will be as follows:

http://s0.cyberciti.org/foo/bar/optimized/test.png

Alternatively, you can overwrite existing PNG images:

1
2
3
4
5
#!/bin/bash
cd /path/to/png/storage/2010/01/c/
## Overwrite images ##
## Keep file system permission and make a backup of original PNG (see options below)  ##
for i in *.png; do optipng -o5 -quiet -keep -preserve -log optipng.log "$i"; done

Next, purge all images from your CDN account. See how to purge old images / invalidating object from cloud front for more information.

Other png optimization tools for Linux / Unix

  • pngcrush - optimizes PNG (Portable Network Graphics) files.
  • pngnq - tool for optimizing PNG (Portable Network Graphics) images. It is a tool for quantizing PNG images in RGBA format.
  • pngquant - PNG (Portable Network Graphics) image optimising utility. It is a command-line utility for converting 24/32-bit PNG images to paletted (8-bit) PNGs.
  1. A guide to PNG optimization.
  2. Visit optipng home page to grab the latest version.
  3. Analyze and optimize your website with PageSpeed tools.
  4. 6 Tools To Find Out Website Load Speed.

Social Profiles

Alvistec spanish
Plan Alvistec in Youtube
Privacy policy

This web site uses third's cookies and the Google's cookies to offer services, personalize ads and analyze traffic (Google analytics).
The information on the use of the this web site is shared with Google and another ads provider platforms. When you using this web site, you accept cookies use.
See more about the Cookies

This web accept cryptocoin donations
BTC address, LTC, ETH, Uphold:
Why donations?