Saturday, April 19, 2008

ImageMagick Bulk Image convert Fun

ImageMagick can do an amazing number of command line manipulation functions. ImageMagick's convert command, for example, can convert an image from one format to another and do a bunch of transformations along the way. Recently I needed to convert a whole bunch of SVG files deeply nested in a directory structure into transparent PNGs. convert, combined with a little bash magick, makes this a quick and painless task:



Let's break that down.


  1. Recursively find every file ending in .svg and pass each one separately to exec

  2. For each exec invoke a shell that executes a command from the given string with the filename getting passed as ${1}

  3. In the command call convert, changing white in the SVG to transparent in the PNG where the first argument is the SVG to convert, and the second argument is the destination PNG filename



Job complete.

Massage Rails better_nested_set full_set into a hierarchy - XML

In a previous post I showed how to convert a betternestedset full_set into a JSON hierarchy that you might feed to a javascript or flash component. Another thing you might want to do is to massage the data into an XML hierarchy to feed a ... javascript or flash component. Now you could use similar code to the previous example and build the XML document up with REXML or another XML library and be done with it. You may, however, want to avoid using such an XML library to avoid the speed penalty that comes with it. Here is an example of converting the content returned from full_set into an XML document without using an XML library: