Converting image to Grayscale

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
Atirag
Posts: 6
Joined: 2012-03-28T13:35:51-07:00
Authentication code: 8675308

Converting image to Grayscale

Post by Atirag » 2012-03-28T14:00:18-07:00

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!

el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Converting image to Grayscale

Post by el_supremo » 2012-03-28T14:59:10-07:00

I haven't tried this yet but I think it will work:

Code: Select all

status = MagickSetColorspace(image_wand,GRAYColorspace);
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

Atirag
Posts: 6
Joined: 2012-03-28T13:35:51-07:00
Authentication code: 8675308

Re: Converting image to Grayscale

Post by Atirag » 2012-03-30T09:10:01-07:00

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.

Atirag
Posts: 6
Joined: 2012-03-28T13:35:51-07:00
Authentication code: 8675308

Re: Converting image to Grayscale

Post by Atirag » 2012-03-30T10:02:57-07:00

Managed to do it with this

Code: Select all

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

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Converting image to Grayscale

Post by anthony » 2012-04-23T22:40:38-07:00

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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

Post Reply