Page 1 of 1

Converting image to Grayscale

Posted: 2012-03-28T14:00:18-07:00
by Atirag
Hello, :)

I'm fairly new to ImageMagick and I am wandering if there's an easy want to convert a normal RGB image to Grayscale. What I need to do is read the image from a file convert it into the grayscale version and write it back. Is there any MagickWand methods to do this. I've been looking through the internet but I can't seem to find how to do it.

I just read the image like this

Code: Select all

MagickWandGenesis();

image_wand=NewMagickWand();

status=MagickReadImage(image_wand,file);
But now what method or technique can I use to convert is? Thanks for the help!

Re: Converting image to Grayscale

Posted: 2012-03-28T14:59:10-07:00
by el_supremo
I haven't tried this yet but I think it will work:

Code: Select all

status = MagickSetColorspace(image_wand,GRAYColorspace);
Pete

Re: Converting image to Grayscale

Posted: 2012-03-30T09:10:01-07:00
by Atirag
Hello Pete, thanks for the reply.

I tried that but when I save the image to disk it is exactly the same as it was before, the colors didn't change.

Re: Converting image to Grayscale

Posted: 2012-03-30T10:02:57-07:00
by Atirag
Managed to do it with this

Code: Select all

  MagickQuantizeImage(image_wand,256,GRAYColorspace,0,MagickFalse,MagickFalse);
Thx!

Re: Converting image to Grayscale

Posted: 2012-04-23T22:40:38-07:00
by anthony
MagickSetColorspace() would probably be like -set colorspace
It sets the colorspace of the image but does not change its values!

To change values use
MagickTransformImageColorspace(wand, colorspace)


You can verify the functions action generally by looking at the MagickCore function of the same name, but without the 'Magick' prefix.