0

Python and Image Magick

Being a old user of Image Magick command line tools, I decided to give a try to magick's python bindings, PythonMagick and PythonMagickWand. PythonMagick is a more complete binding solution which I didn't get the change to try, because there wasn't a package for Ubuntu Gutsy (and I didn't manage to compile the source code in under 2 minutes, so I gave up :)

On the other hand PythonMagickWand uses ctypes to bind with Image Magick. The interface is not pythonic at all, the the library seems to have a lot of rough edges but it does resize! And it provides the great quality results of Image Magick!

Here is a small example

from PythonMagickWand import *
MagickWandGenesis()
wand = NewMagickWand()
MagickReadImage(wand, 'test.png')
MagickScaleImage(wand,800,600)
MagickWriteImage(wand, "out.png")

Not exactly what we except from an OO language, but I can live with that, until Ubuntu Heron releases, which does have PythonMagick in repositories :)

PythonMagickWand is an one-file python module, which means you can transfer it with ease to your hosting provider and start using it at no time.

Tips:

  • You should not try to 'import PythonMagickWand' or you will get a bunch of 'undefined symbols'. Use 'from PythonMagickWand import *' instead.
  • Don't forget to change ctypes.CDLL in line 76 to the correct path. For ubuntu users, and maybe others, the correct line is
 
_magick = ctypes.CDLL('/usr/lib/libWand.so.9')
 
  • Seems that when you use MagickReadImage command to read an image, wand doesn't get overwritten. Remember reset wand variable when you are done with an image.
 wand = NewMagickWand()