Page 1 of 1

Convert greyscale PNG to transparent PNG

Posted: 2010-11-23T01:36:03-07:00
by bloomfoundry
My apologies if this is a simple question, but I've been trolling around the site looking at examples for a few hours and haven't come up with anything yet.

I've got a greyscale image with a gradient of colors:
Image

I want to correlate those gradient values to transparency values, and create a new png image from it. So in the example above, the center would be fully transparent, and it would get progressively less transparent as the image fades toward the edges.

Again, my apologies if this is simple, and many thanks.

Kyle

Re: Convert greyscale PNG to transparent PNG

Posted: 2010-11-23T05:40:42-07:00
by cedk
Hi,

I'm new on this forum anf french (sorry for my english).
What about :

Code: Select all

convert blackandwhite.png -transparent white transparent.png
Tell me

Re: Convert greyscale PNG to transparent PNG

Posted: 2010-11-23T08:52:44-07:00
by bloomfoundry
Thanks so much for your response - that's really close! It gets the white transparent, but I want to get a graduated transparency. So the center should be white, but the pixels that are 90% white should have 90% transparency. That's the part I'm having trouble with - any suggestions?

Re: Convert greyscale PNG to transparent PNG

Posted: 2010-11-23T10:55:52-07:00
by GreenKoopa
I can think of two options to add an alpha channel:

Code: Select all

convert in.png -alpha copy out_1.png
convert in.png +clone -alpha off -compose Copy_Opacity -composite out_2.png
But these probably give you the opposite of what you want, just negate if needed:

Code: Select all

convert in.png -alpha copy -channel A -negate out_neg1.png
In general, you could separate the red, green, and blue layers, add an alpha layer, and recombine. Here, I copied the green channel to create an alpha:

Code: Select all

convert in.png -separate -clone 1 -channel rgba -combine out_neg2.png
http://www.imagemagick.org/Usage/compose/#copyopacity
http://www.imagemagick.org/Usage/color_basics/#channels

Re: Convert greyscale PNG to transparent PNG

Posted: 2010-11-23T11:22:43-07:00
by fmw42
IM only seems to support transparency in 32-bit PNG. There are still bugs in limitations to 8-bit with transparency and grayscale with transparency for PNG. So this works, but requires converting to 32-bit

convert 1.png -alpha off -alpha copy -type truecolormatte PNG32:1_alpha.png


see
http://www.imagemagick.org/Usage/formats/#png

Re: Convert greyscale PNG to transparent PNG

Posted: 2010-11-23T11:50:42-07:00
by GreenKoopa
fmw42, I was unaware of that PNG limitation, so thank you for the warning. I've also read somewhere that -clone must be used in parentheses, but I haven't (yet) had problem with that either.

Image

Re: Convert greyscale PNG to transparent PNG

Posted: 2010-11-23T12:59:03-07:00
by fmw42
The command above does not need parenthesis or clones, but if you do use them for some other command, it is better to use them in parenthesis otherwise you may get unpredicable results.

see
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#sequence (for clones)

Re: Convert greyscale PNG to transparent PNG

Posted: 2010-11-23T17:38:42-07:00
by anthony
fmw42 wrote:

Code: Select all

convert 1.png -alpha off -alpha copy -type truecolormatte PNG32:1_alpha.png
The -alpha off is not needed in the above. (it is only needed for this type of processing when used with -compose CopyOpacity)

also -alpha shape is recommended as it will also replace the color of the mask.

Code: Select all

  convert 1.png -background black -alpha copy -type truecolormatte PNG32:1_alpha.png
See IM Examples, Masking, Controlling Image transparency
http://www.imagemagick.org/Usage/masking/#alpha

Re: Convert greyscale PNG to transparent PNG

Posted: 2010-11-23T18:56:56-07:00
by bloomfoundry
That's exactly what I was looking for - thanks so much!