0

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

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.)

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156

1 Answers1

1

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Sorry for the confusion. I want type to be "TrueColor". I am not concerned with the alpha layer or colorspace at this point. – Michael Steinberg Sep 27 '16 at 19:15
  • I think it is the same problem as http://stackoverflow.com/a/35010805/2836621 Maybe @Glenn-Randers-Pehrson will be able to shed some light... – Mark Setchell Sep 27 '16 at 19:33
  • The "property "png:IHDR.color-type" is 6 for both tells you how the image is actually stored in your PNG file. "6" means RGBA, which is what you want. The colorspace reported by "identify" reflects the content of the image; if all pixels are gray even though stored as RGBA, "identify" reports "Grayscale". If you must have ImageMagick thinking it's RGBA (why?), you'll have to put a colored pixel in there, as you already observed. – Glenn Randers-Pehrson Sep 27 '16 at 22:54