Which API to use to force Grayscale to convert RGB?

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
beginner
Posts: 15
Joined: 2016-05-12T18:21:50-07:00
Authentication code: 1151

Which API to use to force Grayscale to convert RGB?

Post by beginner » 2017-12-03T22:05:34-07:00

The color space of a picture is Grayscale, the file name is gray.jpg
Use convert.exe-type truecolor to convert RGB color space from Grayscale.
convert gray.jpg -type truecolor gray-rgb.jpg

However, I now need to use the ImageMagick API to complete the conversion:

Code: Select all

MagickWand * mw = NewMagickWand ();                
MagickReadImage (mw, "gray.jpg");
                
MagickSetImageType (mw, TrueColorType);
MagickSetImageColorspace (mw, sRGBColorspace);

MagickSetImageFormat(mw, "JPEG");
MagickWriteImage (mw, "gray-rgb.jpg")
The color space of the newly generated gray-rgb.jpg remains the same, still Grayscale.

google the relevant information, find the same problem, but did not find the answer:
https://www.imagemagick.org/discourse-s ... php?t=6888


Which ImageMagick API to use to force Grayscale to convert RGB?

Thank you

User avatar
fmw42
Posts: 23906
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Which API to use to force Grayscale to convert RGB?

Post by fmw42 » 2017-12-03T23:01:19-07:00

In command line:

-define colorspace:auto-grayscale=on|off

prevent automatic conversion to grayscale inside coders that support grayscale. This should be accompanied by -type truecolor. PNG and TIF do not need this define. With PNG, just use PNG24:image. With TIF, just use -type truecolor. JPG and PSD will need this define.

Sorry I do not know the API equivalent. You must also have 6.9.2-0 or higher for that define.

beginner
Posts: 15
Joined: 2016-05-12T18:21:50-07:00
Authentication code: 1151

Re: Which API to use to force Grayscale to convert RGB?

Post by beginner » 2017-12-03T23:41:22-07:00

I use im6.8.9

If it is png format pictures:
MagickSetOption(mw, "png:color-type", "6"); //ok to RGB

but, jpg format pictures:
MagickSetOption(mw, "colorspace:auto-grayscale", "off"); //No effect

snibgo
Posts: 10736
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Which API to use to force Grayscale to convert RGB?

Post by snibgo » 2017-12-03T23:45:07-07:00

Code: Select all

MagickSetImageProperty (magick_wand, "colorspace:auto-grayscale", "off");
snibgo's IM pages: im.snibgo.com

beginner
Posts: 15
Joined: 2016-05-12T18:21:50-07:00
Authentication code: 1151

Re: Which API to use to force Grayscale to convert RGB?

Post by beginner » 2017-12-03T23:53:14-07:00

Thank you very much for yours reply

MagickSetOption(mw, "colorspace:auto-grayscale", "off"); //No effect
MagickSetImageProperty (magick_wand, "colorspace:auto-grayscale", "off"); //No effect

Is my im version too low?

After upgrading to the new version, I found a lot of code needs to be modified, so 6.8.9 has been used.

snibgo
Posts: 10736
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Which API to use to force Grayscale to convert RGB?

Post by snibgo » 2017-12-04T00:00:53-07:00

I'm using 6.9.3-7. That define was introduced for exactly the problem you have, that some software needs grayscale image files to have three channels.
snibgo's IM pages: im.snibgo.com

beginner
Posts: 15
Joined: 2016-05-12T18:21:50-07:00
Authentication code: 1151

Re: Which API to use to force Grayscale to convert RGB?

Post by beginner » 2017-12-04T04:00:47-07:00

viewtopic.php?t=28453

-----------------------------------------------------------
According to the changelog

2015-07-25 6.9.2-0 Dirk Lemstra <dirk@lem.....org>
Added "-set colorspace:auto-grayscale=false" that will prevent automatic conversion to grayscale inside coders that support grayscale.
-----------------------------------------------------------

Seen from here, im6.8.9 is not supported.

Thank you again.

Post Reply