Converting grayscale PNG to TrueColor looses alpha

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
nitech
Posts: 12
Joined: 2007-11-23T13:57:17-07:00

Converting grayscale PNG to TrueColor looses alpha

Post by nitech »

Hi,

I've searched the forum in and out and I cannot find a good answer to the following: I use the ImageMagick DLL on Windows and I try to convert a png file from grayscale to truecolor while maintaining the alpha transparency.

I've tried various parameters, but the background either becomes gray or black - not transparent. If I just leave the image in grayscale, it is transparent - but I need to convert it to TrueColor.

Any ideas?

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

Re: Converting grayscale PNG to TrueColor looses alpha

Post by fmw42 »

Post your exact command and a link to your input image, so others can test. Also what version of IM are you using. If old, perhaps you should upgrade.

this should work but does not seem to on my test image

convert transparentgrayimage -type truecolormatte PNG32:transparentrgbimage

This may be a bug.
nitech
Posts: 12
Joined: 2007-11-23T13:57:17-07:00

Re: Converting grayscale PNG to TrueColor looses alpha

Post by nitech »

Sorry, fmw42 - here are the deatils.

ImageMagick 6.5.6-1 2009-09-14 Q16 OpenMP

I am resizing a high res alpha transparent png to a small one which I then convert to swf using png2swf.exe. The exe says it requires TrueColor png's. I thought the source image was grayscale, but it seems it becomes grayscale during the ImageMagick resize operation.

Source Image: http://www.matchfashion.no/upload_image ... 3EDBCF.png
identify -verbose: http://webadmin.avento.no/temp/verbose_original.txt

The following code creates the following image:

Code: Select all

	strResult = img.Convert(strSourceFile, _
	"-size","130x130", _
	"-resize","130x130", _	
	strDestFile & "_swf_small." & sFile_type)
Image

And this code creates the following image:

Code: Select all

	strResult = img.Convert(strSourceFile, _
	"-size","130x130", _
	"-resize","130x130", _	
	"-type","TrueColor", _	
	strDestFile & "_swf_small." & sFile_type)
Image

I've tried various variations with -channel RGBA, -alpha on and some other parameters with little success.
nitech
Posts: 12
Joined: 2007-11-23T13:57:17-07:00

Re: Converting grayscale PNG to TrueColor looses alpha

Post by nitech »

I found a solution to the problem. Searching the forum, I found a _more verbose_ output for the PNG engine that gave me some details on the original and destination image:

Code: Select all

identify -verbose -debug coder -log %e "\\..\prod_images\doc_136_1_swf_small.png"
identify -verbose -debug coder -log %e "\\..\upload_images\0F9C136E01254D80B19B5EDFD93EDBCF.png"
This report said many things, but one caught my attention: PNG color_type: 6, bit_depth: 8. I then entered this into my convert-command:

Code: Select all

	strResult = img.Convert(strSourceFile, _
	"-size","130x130", _
	"-resize","130x130", _
	"-define","png:color-type=6", _
	strDestFile & "_swf_small." & sFile_type)
Which gave the result I wanted - png2swf now accepts the file format and outputs an alpha-transparent swf.

So - this is not really an answer to my question as I thought the issue was to convert the image to TrueColor. Or per haps png:color-type=6 actually does just that?

Can you shed some light on this as to inform future visitors what just happened - or what they should have done?
nitech
Posts: 12
Joined: 2007-11-23T13:57:17-07:00

Re: Converting grayscale PNG to TrueColor looses alpha

Post by nitech »

I tested your code and it too worked. Thanks :-)

Would you say your PNG32:filename does the same as my "-define","png:color-type=6"?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting grayscale PNG to TrueColor looses alpha

Post by fmw42 »

convert 0F9C136E01254D80B19B5EDFD93EDBCF.png -resize 130x130 -type truecolormatte -define png:bit-depth=8 -define png:color-type=6 PNG32:0F9C136E01254D80B19B5EDFD93EDBCF_resize_test1.png

identify -verbose 0F9C136E01254D80B19B5EDFD93EDBCF_resize_test1.png
Image: 0F9C136E01254D80B19B5EDFD93EDBCF_resize_test1.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 47x130+0+0
Resolution: 72x72
Print size: 0.652778x1.80556
Units: Undefined
Type: GrayscaleMatte
Base type: GrayscaleMatte
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
gray: 8-bit
alpha: 8-bit
Channel statistics:
Gray:
min: 0 (0)
max: 255 (1)
mean: 96.063 (0.376718)
standard deviation: 50.4277 (0.197756)
kurtosis: 0.405082
skewness: -0.511301
Alpha:
min: 0 (0)
max: 255 (1)
mean: 174.434 (0.684055)
standard deviation: 113.362 (0.444558)
kurtosis: -1.29288
skewness: 0.792671



I don't think IM will change it to Red,Green,Blue as it is aleady colorspace RGB, but all channels are the same, you it knows it is really grayscale and only shows once channel (gray) even thought there are 3 R=G=B=gray.

But one of the IM developers should give the final word on this.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting grayscale PNG to TrueColor looses alpha

Post by fmw42 »

I've tried various parameters, but the background either becomes gray or black - not transparent. If I just leave the image in grayscale, it is transparent - but I need to convert it to TrueColor.
If this is still the problem, then to save the transparency, just add -type truecolormatte. The matte part says it has transparency.

My reply above shows that the alpha channel is still there.


convert image -type truecolormatte PNG32:result

The PNG32: says make the result RGB with alpha channel (32-bits, 8-bits for each channel)

This command seems to work just fine for me on IM 6.6.5-8 Q16 (hdri) Mac OSX Tiger


convert 0F9C136E01254D80B19B5EDFD93EDBCF.png -resize 130x130 -type truecolormatte PNG32:0F9C136E01254D80B19B5EDFD93EDBCF_resize.png
Post Reply