Python bindings for ImageMagick’s MagickWand API

I’ve been spending some of my spare time recently putting together some Python bindings for ImageMagick’s MagickWand API. While most bindings I’ve seen just look like generated ctypes code from C header files, these bindings are more object-oriented, completely hiding the MagickWand methods. While this is still in the early stages, it’s workable in a way which I hope is expected and unsurprising:

from pythonmagick.image import Image
from pythonmagick.color import BLUE
i = Image('foo.jpg')
i.format = 'PNG'
i.rotate(45, BLUE)
i.save('flip.png')

The above reads in a JPEG image, reformats to PNG and rotates by 45 degrees with a background colour of blue. The image is then saved to a new file.

This may look a lot like PIL code, so why not use it instead of ImageMagick? ImageMagick covers a much wider range of image formats than PIL, and supports better algorithms for image resizing.

Check out the bindings by installing them with easy_install:

easy_install http://svn2.assembla.com/svn/pythonmagickwand/trunk/

On a side note, I’m quite impressed with Assembla, the site which I’m using for my SVN and Trac repo. It’s about as good as something I would deploy myself, but with much less hassle. Honestly, these days there’s less and less reason for me to spend hours setting up stuff myself, and that’s a great thing.

  1. Ben

    September 29, 2008 at 3:56 am

    from pythonmagickwand.image import Image
    from pythonmagickwand.color import BLUE
    i = Image(‘test.jpg’)
    i.format = ‘PNG’
    i.rotate(45, BLUE)
    i.save(‘flip.png’)

    looks like it’s renamed.

  2. Frank Borell

    November 24, 2009 at 1:05 pm

    Wondering if you could help my installation on windows. I’m getting the _getstate_ error below when doing the dry-run or easy_install.exe http://svn2.assembla.com/svn/pythonmagickwand/trunk.
    Thanks in advance …
    Frank

    C:\Python26\Scripts>easy_install.exe –dry-run http://svn2.assembla.com/svn/pyth
    onmagickwand/trunk
    Downloading http://svn2.assembla.com/svn/pythonmagickwand/trunk
    Doing subversion checkout from http://svn2.assembla.com/svn/pythonmagickwand/tru
    nk to c:\docume~1\191203\locals~1\temp\easy_install-auwech\trunk
    Processing trunk
    Running setup.py -n -q bdist_egg –dist-dir c:\docume~1\191203\locals~1\temp\eas
    y_install-auwech\trunk\egg-dist-tmp-ytmat2
    Traceback (most recent call last):
    File “C:\Python26\Scripts\easy_install-script.py”, line 8, in
    load_entry_point(‘setuptools==0.6c11′, ‘console_scripts’, ‘easy_install’)()
    File “c:\python26\lib\site-packages\setuptools\command\easy_install.py”, line
    1712, in main
    with_ei_usage(lambda:
    File “c:\python26\lib\site-packages\setuptools\command\easy_install.py”, line
    1700, in with_ei_usage
    return f()
    File “c:\python26\lib\site-packages\setuptools\command\easy_install.py”, line
    1716, in
    distclass=DistributionWithoutHelpCommands, **kw
    File “C:\Python26\lib\distutils\core.py”, line 152, in setup
    dist.run_commands()
    File “C:\Python26\lib\distutils\dist.py”, line 975, in run_commands
    self.run_command(cmd)
    File “C:\Python26\lib\distutils\dist.py”, line 995, in run_command
    cmd_obj.run()
    File “c:\python26\lib\site-packages\setuptools\command\easy_install.py”, line
    211, in run
    self.easy_install(spec, not self.no_deps)
    File “c:\python26\lib\site-packages\setuptools\command\easy_install.py”, line
    422, in easy_install
    return self.install_item(None, download, tmpdir, deps, True)
    File “c:\python26\lib\site-packages\setuptools\command\easy_install.py”, line
    476, in install_item
    dists = self.install_eggs(spec, download, tmpdir)
    File “c:\python26\lib\site-packages\setuptools\command\easy_install.py”, line
    655, in install_eggs
    return self.build_and_install(setup_script, setup_base)
    File “c:\python26\lib\site-packages\setuptools\command\easy_install.py”, line
    930, in build_and_install
    self.run_setup(setup_script, setup_base, args)
    File “c:\python26\lib\site-packages\setuptools\command\easy_install.py”, line
    919, in run_setup
    run_setup(setup_script, args)
    File “c:\python26\lib\site-packages\setuptools\sandbox.py”, line 52, in run_se
    tup
    pr_state = pkg_resources.__getstate__()
    AttributeError: ‘module’ object has no attribute ‘__getstate__’

  3. Frank Borell

    November 25, 2009 at 3:11 pm

    Ian,
    I tried with Python 2.51, then reinstalled to 2.5.4. I ran easy_install from the subversion directory(missing in PATH) and it seemed to install. I did run the Image Magik win32 install just prior-(ver 6.5.8 Q16). I’m able to import pythonmagickwand but not the .image class. Is the “zip_safe flag not set” affecting the process ?

    Frank

    >>> import pythonmagickwand
    >>> dir(pythonmagickwand)
    ['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__path__']

    C:\Program Files\Subversion\bin>c:\python25\scripts\easy_install.exe -d c:\pytho
    n25\lib\site-packages http://svn2.assembla.com/svn/pythonmagickwand/trunk
    Downloading http://svn2.assembla.com/svn/pythonmagickwand/trunk
    Doing subversion checkout from http://svn2.assembla.com/svn/pythonmagickwand/tru
    nk to c:\docume~1\admini~1\locals~1\temp\2\easy_install-nmdws-\trunk
    Processing trunk
    Running setup.py -q bdist_egg –dist-dir c:\docume~1\admini~1\locals~1\temp\2\ea
    sy_install-nmdws-\trunk\egg-dist-tmp-z6jqnr
    zip_safe flag not set; analyzing archive contents…
    pythonmagickwand 0.1 is already the active version in easy-install.pth

    Installed c:\python25\lib\site-packages\pythonmagickwand-0.1-py2.5.egg
    Processing dependencies for pythonmagickwand==0.1
    Finished processing dependencies for pythonmagickwand==0.1

    C:\Program Files\Subversion\bin>python
    ActivePython 2.5.4.4 (ActiveState Software Inc.) based on
    Python 2.5.4 (r254:67916, Apr 27 2009, 15:41:14) [MSC v.1310 32 bit (Intel)] on
    win32
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import pythonmagickwand
    >>> dir(pythonmagickwand)
    ['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__path__']
    >>> from pythonmagickwand.image import Image
    Traceback (most recent call last):
    File “”, line 1, in
    File “C:\Python25\lib\site-packages\PIL\image.py”, line 2, in
    # The Python Imaging Library.
    File “build\bdist.win32\egg\pythonmagickwand\api.py”, line 21, in
    ImportError: Cannot find ImageMagick MagickWand library.

  4. Ian Stevens

    November 28, 2009 at 7:46 pm

    Sorry, Frank, I think I completely missed that you were trying to install PythonMagickWand on Windows. Do you have ImageMagick installed? (See http://www.imagemagick.org/script/binary-releases.php#windows if you don’t.) There might be some problems finding the location of the MagickWand library on Windows. My wrapper tries to intelligently locate the library, but it might not be smart enough for Windows. Actually, I’m pretty sure it isn’t. Do you know where the MagickWand library files are installed on Windows? You could try editing the pythonmagickwand/api.py at line 15 to include the path. If you get it working, please send me a patch. Sorry, I don’t have easy access to a Windows machine to test this.

  5. Chris

    January 6, 2010 at 12:03 pm

    I tried adding r’C:\Program Files\ImageMagick-6.5.8-Q16′ to the line you mention, but now I get the error:

    from pythonmagickwand.image import Image
    File “C:\Program Files\Python26\lib\site-packages\pythonmagickwand\image.py”, line 2, in
    from pythonmagickwand import api, wand, color
    File “C:\Program Files\Python26\lib\site-packages\pythonmagickwand\api.py”, line 23, in
    _lib = CDLL(wand_lib)
    File “C:\Program Files\Python26\lib\ctypes\__init__.py”, line 353, in __init__
    self._handle = _dlopen(self._name, mode)
    WindowsError: [Error 5] Access is denied

  6. Ian Stevens

    January 19, 2010 at 8:40 pm

    Chris, are you still having issues? Sorry for the late reply. It looks like a Windows permissions error. Double-check that you have read and execute permissions on C:\Program Files\ImageMagick-6.5.8-Q16. If not, add them. I’m not sure why that wouldn’t already be the case, but stranger things have happened. Sorry, I don’t have access to a Windows machine for testing.

  7. Nicolas

    December 6, 2010 at 7:24 am

    Hey, another Windows user here, if you’re still there. I also got the import error, but when I tried to directly specify the library (wand_lib = ‘C:\Program Files (x86)\ImageMagick-6.6.6-Q8\CORE_RL_wand_.dll’), I get this error:

    Traceback (most recent call last):
    File “C:\Admin\Python\imagemagick.py”, line 1, in
    from pythonmagickwand.image import Image
    File “build\bdist.win32\egg\pythonmagickwand\image.py”, line 2, in
    # The Python Imaging Library.
    File “build\bdist.win32\egg\pythonmagickwand\api.py”, line 663, in
    File “C:\Admin\Programs\Python27\lib\ctypes\__init__.py”, line 366, in __ge
    tattr__
    func = self.__getitem__(name)
    File “C:\Stratton\Programs\Python27\lib\ctypes\__init__.py”, line 371, in __ge
    titem__
    func = self._FuncPtr((name_or_ordinal, self))
    AttributeError: function ‘AnimateImages’ not found

Leave a Reply