Re: PythonMagick: Convert image to gray scale (black & white)

Osmo Maatta <[email protected]>
Newsgroups gmane.comp.video.image-magick.user
Message-ID <[email protected]>
Hi,
Never mind people.

I've started to use PythonMagickWand.py which is a ctypes binding to 
MagickWand.
Ref.: http://www.procoders.net/index.php?s=pythonmagick

Very nice solution because it reveals all MagickWand fucntions to Python.
The manual: http://www.imagemagick.org/script/magick-wand.php

I've earlier used MagickWand in Gscreendump
http://code.google.com/p/gscreendump/source/browse/trunk/src/sd_imagemagick.c

One thing though,
PythonMagickWand.py lacks a definition for PixelSetColor() function so I 
added it to my local copy.

# bool PixelSetColor( PixelWand pxl_wnd, string imagemagick_col_str )
# Ref: http://www.magickwand.org/PixelSetColor.html
<http://www.magickwand.org/PixelSetColor.html>
try:
     _magick.PixelSetColor.restype = MagickStatusType
     _magick.PixelSetColor.argtypes = (PixelWand, 
ctypes.POINTER(ctypes.c_char), )
except AttributeError,e:
     print e
else:
     PixelSetColor = _magick.PixelSetColor
-----------------

Here is a prototype how I use the MagickWand binding from Python.

(FLIP_VERTICAL,
  FLIP_HORIZONTAL) = range(2)

def magickwand_flip_image(wand, direction):
     # Image name
     filename = MagickGetImageFilename(wand)

     if direction == FLIP_VERTICAL:
         status = MagickFlipImage(wand)
     else:
         # direction == FLIP_HORIZONTAL
         status = MagickFlopImage(wand)

     if status.value != True:
         err = magickwand_get_err_msg(wand)
         err_msg = "ImageMagick: Cannot flip the image %s. %s.\n" % ( 
filename, err,)
         raise ImageMagickException(err_msg)

     return wand


def magickwand_call_function(filename_from, filename_to, func_cb, *args):

     ret = False

     try:
         # Open the image
         wand = NewMagickWand()
         status = MagickReadImage(wand, filename_from)

         if status.value != True:
             err = magickwand_get_err_msg(wand)
             MagickClearException(wand)
             err_msg = "ImageMagick: Cannot open image %s. %s.\n" % 
(filename_from, err,)
             raise ImageMagickException(err_msg)


         # Call the MagickWand function.
         func_cb(wand, *args)

         MagickClearException(wand)

         # Save the image

         # This seq faults if the file is write protected.
         # status = MagickWriteImage(image, filename);

         if filename_from == filename_to or filename_to == None:
             filename = filename_from
         else:
             filename = filename_to

         # This is more manageable
         status = MagickWriteImages(wand, filename, MagickTrue)

         if status.value != True:
             err = magickwand_get_err_msg(wand)
             MagickClearException(wand)
             err_msg = "ImageMagick: Cannot write to %s. %s.\n" % 
(filename_from, err,)
             raise ImageMagickException(err_msg)

         ret = True

     except Exception, e:
         import sys
         sys.stderr.write("%s\n" % (e.msg,))

     finally:
         DestroyMagickWand(wand)

     return ret

# Test it:
ret = magickwand_call_function("test1.png", "test2.png", 
magickwand_flip_image, FLIP_VERTICAL)
print "ret=", ret

ret = magickwand_call_function("test1.png", "test3.png", 
magickwand_flip_image, FLIP_HORIZONTAL)
print "ret=", ret
--------------

Many thanks to those who developed PythonMagickWand.py!

Kindly
   Osmo (Moma) Antero
   Grønland
   http://www.futuredesktop.org (http://www.futuredesktop.com)

<http://www.magickwand.org/PixelSetColor.html>


On 03/24/2010 07:39 AM, Osmo Maatta wrote:
> Hello,
> I want to use PythonMagick to convert an image to gray scale.  I've 
> read the documentation of Magick++ on 
> http://www.imagemagick.org/Magick++ but cannot figure out how to 
> access the GrayscaleType or GRAYColorspace attributes from Python.  
> Please help.
>
> Sample code:
>
> import PythonMagick
>
> filename = "test1.png"
> img = PythonMagick.Image()
> img.read(filename)
>
> img.type = GrayscaleType
>
> #img.quantizeColorSpace(??)
> #img.quantize()
> #img.channel(PythonMagick.ChannelType.RedChannel ??)
>
> img.write("test2.png")
> ----
>
> This system is: 64bit Ubuntu 9.10.
>
> $ apt-cache show python-pythonmagick
> python-pythonmagick - Object-oriented Python interface to ImageMagick
> Version: 0.9.1-1ubuntu1
> ----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.