Python and ImageMagick

UPDATE 2013-10-27: Wand continues to have frequent updates and now supports sequences, line/text drawing, reading EXIF, chops, and some effects among other things. You can keep track w/ the latest CHANGELOG. I can highly recommend it if it does what you need.

I’m a big Python fan and it’s my preferred language these days, but sometimes you just have to shake your head and give up. One area where Python falls down is in its ImageMagick bindings. You can read more about the history of some of the libs here.

Here’s my experiences on OS X (10.6, 10.8) and MacPorts that hopefully will save some people time:

  • PythonMagick (BROKEN) – the official bindings, and somewhat up-to-date (last update 2012-09-19), I could get it to compile, but it threw TypeErrors when trying to run. You can compile it like so:

    ./configure --prefix=/opt/local CPPFLAGS="-I/opt/local/include -I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7" LDFLAGS=-L/opt/local/lib

  • pythonmagickwand (BROKEN) – this shows up in searches, but it hasn’t been updated in 5 years. It’s as you can imagine, broken.
  • python-magickwand (BROKEN) – last updated 2012-01-22, it includes a nice README (w/ the aforementioned history) and examples and I wanted it to work, alas, I couldn’t get it to.
  • Wand (WORKS!) – Wand is under active development (last commit 2013-01-31), is Pythonic, has community contributions, and works! Sounds perfect, right? Alas, it doesn’t support many features currently (layers, effects, animation etc) although it’s on the roadmap.
  • pgmagick (WORKS!) – This lib was the closest to doing what I needed. It’s very active (last commit 2013-02-10) and is much more comprehensive than Wand.. however, while supposedly working for ImageMagick, I could only get it working w/ GraphicsMagick, which in my case, was missing features that I needed. There are decent docs but very little real code out there. Here btw, is how I got it running (it has some boost issues; also, pip doesn’t work):

    sudo port install boost
    cd /opt/local/lib
    sudo ln -s libboost_python-mt.a libboost_python.a
    sudo ln -s libboost_python-mt.dylib libboost_python.dylib
    sudo easy_install pgmagick

    After wrestling for quite a while, I threw in the towel and rewrote my code in Ruby w/ RMagick, which is well maintained and up-to-date, has comprehensive documentation, and lots of example code floating around the web.

    If you’re tied to Python, using PIL or subprocess/envoy to call convert/mogrify directly is probably your best bet, but if you are doing anything substantially complex, calling out to an RMagick script will probably save yourself a lot of pain.