Can't convert Grayscale to RGB

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
tony.whittam
Posts: 1
Joined: 2013-06-09T14:33:56-07:00
Authentication code: 6789

Can't convert Grayscale to RGB

Post by tony.whittam »

I have an image in a .PNG file that I am trying to convert from being stored in Grayscale + Alpha space to being stored in RGB + Alpha space. I need to do this because some of the devices I use the image on don't seem to be able to handle Grayscale + Alpha in the .PNG file.

I am trying to use
convert worldwhite3-512.png -type TrueColorMatte -define png:color-type=6 out.png
but the output is still stored as grayscale + alpha.

Perhaps I am misunderstanding something about what this command should do. I want it to write the same image unmodified but stored in RGB + Alpha space. Can anyone advise me whether this is the command is correct? If so then what could be stopping the conversion? I've put more detail below.

I'm using Version: ImageMagick 6.8.5-7 2013-05-19 Q16 running on Windows 7 64-bit.

Identify -verbose on my original file gives

Image: worldwhite3-512.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 512x512+0+0
Resolution: 118.11x118.11
Print size: 4.33494x4.33494
Units: PixelsPerCentimeter
Type: GrayscaleAlpha
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 8-bit
alpha: 8-bit
...

After I try this
convert worldwhite3-512.png -type TrueColorMatte -define png:color-type=6 out.png
identify -verbose on the output file gives.

Image: out.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 512x512+0+0
Resolution: 118.11x118.11
Print size: 4.33494x4.33494
Units: PixelsPerCentimeter
Type: GrayscaleAlpha
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 8-bit
alpha: 8-bit
...

For anyone that might be interested I've put the original image at https://dl.dropboxusercontent.com/u/893 ... e3-512.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Can't convert Grayscale to RGB

Post by fmw42 »

I do not think you can convert a grayscale image to RGB without modifying its values. In IM6 all grayscale images are 3 channels with the R=G=B. But IM knows that all 3 channels are the same and thus labels it as grayscale. If you change say one fully transparent pixel to some color (under the transparency), then it should consider the image as color rather than grayscale.
Last edited by fmw42 on 2013-06-09T17:18:16-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Can't convert Grayscale to RGB

Post by snibgo »

tony.whittam wrote:Identify -verbose on my original file gives ...
You misunderstand "identify". This gives details of the image as it is stored in memory, not as it was in the file.

The file worldwhite3-512.png has three 8-bit channels, plus alpha. I verified this with Gimp and a utility program. When IM reads it, it recognizes all the tones are grey, and reports that.

Your convert command ...

Code: Select all

convert worldwhite3-512.png -type TrueColorMatte -define png:color-type=6 out.png
... results (in IM 6.8.5-8) in three 8-bit channels, plus alpha.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Can't convert Grayscale to RGB

Post by fmw42 »

snibgo wrote:convert worldwhite3-512.png -type TrueColorMatte -define png:color-type=6 out.png
Nevertheless, IM still labels it as type=grayscalealpha.

I am not sure why other tools cannot handle that, since it is still 3 channels. Perhaps they get confused by the 3 channels but labeled grayscale.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Can't convert Grayscale to RGB

Post by fmw42 »

Find perfectly transparent pixel:
511,511: ( 0, 0, 0, 0) #00000000 none

convert WorldWhite3-512.png -size 1x1 -fill "rgba(0%,1%,2%,0)" -draw "color 511,511 point" out2.png
identify -verbose out2.png
Image: out2.png
Class: DirectClass
Colorspace: sRGB
Type: PaletteAlpha
Depth: 8-bit
Alpha: True
Channels: srgba
Rendering intent: Perceptual
Gamma: 0.45455
Post Reply