Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I access ImageMagick API with Python?

I need to use ImageMagick as PIL does not have the amount of image functionality available that I am looking for. However, I am wanting to use Python.

The python bindings (PythonMagick) have not been updated since 2009. The only thing I have been able to find is os.system calls to use the command line interface but this seems clunky.

Is there any way to access the API directly using ctypes and conversions of some sort? As a last resort is there any other library out there that has the extensive amount of image editing tools like ImageMagick that I have looked over?

like image 703
bsktball11ch Avatar asked Oct 25 '11 20:10

bsktball11ch


People also ask

What is ImageMagick in Python?

PythonMagick is the Python binding of the ImageMagick library. ImageMagick® is a free software suite to create, edit, and compose bitmap images. It can read, convert and write images in a large variety of formats.

How do I use ImageMagick on Windows?

You have 2 choices. 1) Use the full path every time you run ImageMagick, i.e. something like "C:\Programs\Image Magick\convert" or 2) Go to Settings -> System -> Advanced -> Environment Variables and click Edit and add the directory in to the start of your PATH.


2 Answers

I would recommend using Wand (explanations follows).

I was looking for proper binding to ImageMagick library, that would:

  • work error/problem free
  • be regularly maintained and up to date
  • allow nice objective Python

But indeed python API (binding) has too many different (mostly discontinued) versions. After reading a nice historical overview by Benjamin Schweizer it has all become clear (also see his github wiki):

  • GraphicsMagick
  • PythonMagick - first implementation
  • PythonMagickWand/Achim Domma - first Wand - a CDLL implementation
  • PythonMagickWand/Ian Stevens
  • MagickFoo - included in python-magickwand
  • Wand/Hong Minhee - the latest project

Now Wand is just a (reduced) C API to the ImageMagick ".. API is the recommended interface between the C programming language and the ImageMagick image processing libraries. Unlike the MagickCore C API, MagickWand uses only a few opaque types. Accessors are available to set or get important wand properties." (See project homepage)

So it is already a simplified interface that is easer to maintain.

like image 150
Yauhen Yakimovich Avatar answered Sep 29 '22 20:09

Yauhen Yakimovich


This has worked for me for the following command to create an image from text for the letter "P":

import subprocess  cmd = '/usr/local/bin/convert -size 30x40 xc:white -fill white -fill black -font Arial -pointsize 40 -gravity South -draw "text 0,0 \'P\'" /Users/fred/desktop/draw_text2.gif'  subprocess.call(cmd, shell=True) 
like image 45
fmw42 Avatar answered Sep 29 '22 18:09

fmw42