image manipulation with python

in webapps you often need to manipulate images. create thumbnails, add shadows or borders, create captchas and many other things. usually i use imagemagick. it’s a very powerfull image manipulation tool with apis for many languages. i often experienced some difficulties when trying to use it with a specific language. there i usually almost no documentation for the apis and it looks like it’s not a lot used. many tend to call the imagemackig executable trough a os call.

how is it with python

i found a few image magick python modules. most were completely undocumented, unfinished or more or less uninstallable (at least on a 64 bit debian). the one who works properly is PythonMagick. i first tried to install the lattest version but due to some dependencies problems it wasn’t possible on my ubuntu system. there is a deb package available which was easyli installed with: sudo apt-get install python-pythonmagick it’s not the latest version but it worked ok, at least for my needs.

how do you use it

i found no documentation for it but it was quite simple to figure out the basic things. there is documentation for Magick++ which is a object oriented wrapper around imagemagick. all the image methods have short descriptions. to add a red 2 pixel border to an image for example you need following code: from PythonMagick import Image i = Image('example.jpg') # reades image and creates an image instance i.borderColor("#ff0000") # sets border paint color to red i.border("2x2") # paints a 2 pixel border i.write("out.jpg") # writes the image to a file the parameter “2×2″ of the border method is a geometry string used by imagemagick as input for many methods. another example who crops the image, flips it, adds an oil paint look and finally save it as a png. i = Image('example.jpg') i.crop("100x100+25+25") i.flip() i.oilPaint(2) i.save('out.png') if i find some spare time i will add a few more complex examples of how to use imagemagick from python.

3 Responses to “image manipulation with python”

  1. shardul Says:

    hi,

    this library is not working anymore. sudo apt-get install python-pythonmagick when i do that it installed it successfully. But after when i try to use it it gives me error with following

    File “thumbnail.py”, line 1, in from PythonMagick import Image File “/var/lib/python-support/python2.6/PythonMagick/__init__.py”, line 1, in import _PythonMagick ImportError: /var/lib/python-support/python2.6/PythonMagick/_PythonMagick.so: undefined symbol: _ZTIN5boost6python15instance_holderE

    Also there has been discussion about this here http://bugs.gentoo.org/273470

    Didnt you faced the same problem?

  2. admin Says:

    i remember having problems with pythonmagick after an os update. i cant actually remember what i did exactly to fix it but it looks like i built it myself from http://www.imagemagick.org/download/python/PythonMagick-0.9.1.tar.bz2 but i also remember that it took a while till it worked, stupidly i didn’t post it here.

  3. Taylor Braun-Jones Says:

    Thanks - helpful!

    FYI - i.save(…) seems to be i.write(…) now

Leave a Reply