Convert greyscale PNG to transparent PNG

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
bloomfoundry

Convert greyscale PNG to transparent PNG

Post 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
cedk
Posts: 16
Joined: 2010-11-23T05:36:22-07:00
Authentication code: 8675308

Re: Convert greyscale PNG to transparent PNG

Post 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
bloomfoundry

Re: Convert greyscale PNG to transparent PNG

Post 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?
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Convert greyscale PNG to transparent PNG

Post 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
Last edited by GreenKoopa on 2010-11-23T11:25:13-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert greyscale PNG to transparent PNG

Post 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
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Convert greyscale PNG to transparent PNG

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert greyscale PNG to transparent PNG

Post 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)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Convert greyscale PNG to transparent PNG

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
bloomfoundry

Re: Convert greyscale PNG to transparent PNG

Post by bloomfoundry »

That's exactly what I was looking for - thanks so much!
Post Reply