Saving some space

🐚

The png file format has become the de-facto lossless file format for pixmaps. One of interesting aspects of this format, is that a given image can be compressed in multiple ways, and that most tools only use one. This means that given the right tools, png files can be made smaller without any loss of quality. On Mac OS X, it is very easy to find all the png files in one’s home directory and reduce their size, assuming your have the Developer Tools and Mac ports installed:

sudo port install optipng
mdfind -0 -onlyin ~ "kMDItemContentType=public.png" | xargs -n 5 -0 optipng -preserve

The first line install the optipng tool.
The second line involves three command-line tools:

  • mdfind searches for all files of type public.png in the home directory (~) separating each item with a null character
  • xargs takes the file paths in bunches of 5 (completely arbitrary value) and passes them as parameters to the next command:
  • optipng compresses the files, preserving the file attributes

To be honest I did not bother checking how much space this save me, I just wanted to play around with mdfind… Adding the parameter -o 3 to optipng could save more space, but increase the processing time. Technically you could run this command on the System directory to compress all the png files there, but I don’t think this would be wise. I realise the same technique could be used for jpeg files using jpegtran, if I have some time, I’ll explain how to do it.

flattr this!