PNG

From Christoph's Personal Wiki
Jump to: navigation, search

PNG (Portable Network Graphics) is a bitmapped image format that employs lossless data compression. PNG was created to improve and replace the GIF format, as an image-file format not requiring a patent license.

Utilities

Images which are not photographs, such as diagrams and screen captures of applications or older video games, use few colors. If it makes sense, save the image in indexed mode. A truecolour PNG can often be converted to indexed mode without changing the look of the image, while saving on file size. (See wikipedia:color depth for information on indexed mode and truecolour.)

It is normal for image editing programs to produce poor PNG compression, even when run with their maximum compression choices. As a result, there are a variety of tools to compress PNGs without any loss of quality. However, if the image will be scaled by Mediawiki before viewing, then these steps are pointless. Some such tools and information on using them is shown below.

OptiPNG is generally better than pngcrush and usually significantly faster. AdvDef can be used after OptiPNG to further improve the results. AdvDef is straightforward to use, as it optimizes only the compression itself.

For quick compression, simply use optipng with no options at all:

optipng file.png

If smallest results are desired and time is not important, a chain of this sort produces even smaller possible results:

optipng -o7 file.png
advdef -z4 file.png
pngout /ks file.png
deflopt file.png

Each of these utilities uses a different, more sophisticated "deflate" compression method variant on the PNG and generally produces a smaller file when run after the other tools. If the smallest result matters, try some experimenting to see what produces the best result.

After any compression, the image should be compared to the original. It's occasionally the case that quirks in the original cause transparency to be lost even in compression which is intended to be lossless. This commonly, but not always, shows up as a change in the background colour which is obviously visible at a glance.

For quick-and-dirty optimization, the opt-png script (found in the littleutils package) might be useful. It automates PNG optimization, utilizing pngcrush and a variant of pngrewrite as underlying engines.

Note also that these chains, particularly the pngrewrite step, will discard non-image blocks, often including copyright or creator details. Check the pngrewrite and other program options if you want to preserve this information.