Paul’s Blog

A blog without a good name

Optimizing PNGs

With the tiles generated normally the next step would be to serve them, but because I’m planning to distribute them to others, I’m going to the unusual step of optimizing the PNGs. Optimizing PNGs can cut the file size in half, helping downstream users of the tiles I’m generating.

To make use of all the cores of my CPU, I’m going to use find to locate the PNGs, then the program parallel to have optipng operate in parallel.

OptiPNG is a program that performs lossless optimization on PNGs. Because low-zoom tiles are more likely to be viewed and there’s fewer of them, I’ll call the program with different options, doing more aggressive optimizations on low-zoom tiles. There’s no magic right answer for much time to spend compressing, but I found these reasonable, and save up to 50% space on some zooms.

1
2
3
find osm_tiles/{0,1,2,3,4,5,6}/ -type f -name '*.png' -print0 | parallel -0 -m optipng -quiet -o4 -strip all
find osm_tiles/{7,8}/ -type f -name '*.png' -print0 | parallel -0 -m optipng -quiet -o2 -strip all
find osm_tiles/{9,10}/ -type f -name '*.png' -print0 | parallel -0 -m optipng -quiet -o1 -strip all

The space used can be measured with du -hsc --apparent-size osm_tiles/*. --apparent-size is essential since many of the tiles will be below the size of one block on disk.

All of this is of course not required, but helps a bit, and is an interesting experiement regardless.