Page 1 of 1

Which API to use to force Grayscale to convert RGB?

Posted: 2017-12-03T22:05:34-07:00
by beginner
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

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

Posted: 2017-12-03T23:01:19-07:00
by fmw42
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.

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

Posted: 2017-12-03T23:41:22-07:00
by beginner
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

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

Posted: 2017-12-03T23:45:07-07:00
by snibgo

Code: Select all

MagickSetImageProperty (magick_wand, "colorspace:auto-grayscale", "off");

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

Posted: 2017-12-03T23:53:14-07:00
by beginner
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.

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

Posted: 2017-12-04T00:00:53-07:00
by snibgo
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.

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

Posted: 2017-12-04T04:00:47-07:00
by beginner
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.