简体   繁体   中英

How can I force Imagemagick to output a grayscale image in RGB format?

This question has been asked previously, but the approved answers do not work for me:

Prevent ImageMagick converting to grayscale

ImageMagick: convert keeps changing the colorspace to Gray. How to preserve sRGB colorspace?

Example image:

convert.exe logo: -type grayscalealpha png32:-|convert - -define png:color-type=6 png32:-|identify -verbose -

The only way to force what I want is to draw a color somewhere in the visible area. This accomplishes what I'm looking to do:

convert.exe logo: -type grayscalealpha png32:-|convert - -fill #11000000 -draw "rectangle 0,0 0,0" -|identify -verbose -

However, I'd prefer not altering the image. I've tried the above on the following builds on Win7:

ImageMagick 7.0.3-1 Q16 x64 2016-09-20
ImageMagick 6.9.0-10 Q16 x64 2015-02-28

Thank you!

EDIT: Just realized I'm drawing a clear pixel on top, so the image shouldn't change, yet it still works. Still feels like a hack though.

Updates:

After working on this some more hours, I realize my question wasn't clear because I wasn't clear on how PNGs are stored.

Problem: When ImageMagick is given an image with only gray colors (R=G=B), it will store the image, I think, with only one channel (gray).

I say "I think" because identify prints out type as "GrayscaleAlpha" and only lists a "Gray" and "Alpha" channel. The output of the second command will list type as "PaletteAlpha" and list Red, Green, Blue and Alpha. However, the property "png:IHDR.color-type" is 6 for both. So I think I may be misunderstanding the meaning of "Type" and "Base Type" used by ImageMagick.

(Also, ImageMagick V6 and V7 seem to have different behaviors with this. For example, v6 lists alpha channel as 1-bit, even though it is 8-bit.)

I am not sure what the actual problem/question is. I assume, from your question title, that you want an output image of Type:Grayscale and Colorspace:sRGB , which you can get like this:

convert logo: -colorspace gray -define png:-color-type=6 a.png; identify -verbose a.png | grep -E "Type:|Colorspace:"

Output

Type: Grayscale
Colorspace: sRGB

As it is, you are overriding the Grayscale with your PNG32: and forcing it to GrayscaleAlpha:

convert logo: -colorspace gray -define png:-color-type=6 PNG32:a.png; identify -verbose a.png | grep -E "Type:|Colorspace:"

Output

Type: GrayscaleAlpha
Colorspace: sRGB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM