# we need PythonMagick for its unsharp mask functions
#
from PIL import Image
from PythonMagick import Image as PMImage #distinguish between PIL and PythonMagick 'Image' object
from PythonMagick import Blob
#Convert Magick image to PIL (Python Imageing Library) image
#taken from ftp://ftp.imagemagick.net/pub/ImageMagick/python/README.txt (which contains an error:
#Image.data does not exist
#see Magick++ documentation for the PythonMagick functions
def convertMGtoPIL(magickimage):
'works with grayscale and color'
img = PMImage(magickimage) # make copy
img.depth = 8 # this takes 0.04 sec. for 640x480 image
img.magick = "RGB"
w, h = img.columns(), img.rows()
blb=Blob()
img.write(blb)
data = blb.data
# convert string array to an RGB Pil image
pilimage = Image.fromstring('RGB', (w, h), data)
return pilimage
#Convert PIL image to Magick image
def convertPILtoMG(pilimage):
'returns RGB image'
if pilimage == None:
return None
if pilimage.mode == 'L':
pilimage = pilimage.convert("RGB")
size = "%sx%s" % (pilimage.size[0], pilimage.size[1])
data = pilimage.tostring('raw','RGB')
mdata = Blob(data)
img = PMImage(mdata, size, 8, 'RGB')
return img
#unsharp mask function, see ImageMagick docs for explanation of radius, sigma, amount and threshold
def usm(pilimage,radius,sigma,amount,threshold):
img=convertPILtoMG(pilimage)
img.unsharpmask(radius,sigma,amount,threshold)
pilimage=convertMGtoPIL(img)
return pilimage
Comments
Why break a butterfly on the wheel when you can write own PIL filter or use builtin ImageFilter.SHARPEN?
#
PIL's Sharpen is NOT the same as unsharp mask. You may have a look at this tutorial for an explanation.
I know you can write your own PIL filter but I can't do that. However there are a ton of other features available in ImageMagick which are not available in PIL. So eventually it is a matter of choice.
#