I have a gray-scale image. I want to convert it into an transparent PNG, such that the black pixels (in the original image) become fully opaque, the white pixels become fully transparent, a 50% grey pixel would become 50% transparent, and so on for all the shades in between.

How can I achieve this?

(Yes, I know that this is almost the same question as Converting grayscale shades into alpha channel, but I want to know how to do this using only command line tools – e.g. ImageMagick or NetPBM.)

Here is a sample result from the above-mentioned question. This sample is a result which I would like to achieve using only command-line tools.


If you open this PNG in a viewer that support transparency, you'll see what I mean.


Ooookay. After plenty of googling, and reading for more of the ImageMagick manual than I actually care for, here's the answer. Given that you have a grayscale image called source.png, here are my commands.

To make the make the black pixels transparent and keeps the white pixels as they are, run this command:

 convert source.png -alpha copy -fx '#fff' result.png

To instead make the white pixels transparent while keeping the black as-is, use:

 convert source.png -alpha copy -channel alpha -negate +channel -fx '#000' result.png

Let's explain that last command a bit more thoroughly:

  • convert – Is the ImageMagic command (one of several)
  • source.png – The greyscale source image.
  • -alpha copy – Copy contents of the previous file into the alpha channel.
  • -channel alpha – Specify that following operators only should affect the alpha channel.
  • -negate – Invert the alpha channel (will, because of the previous -chanel alpha not affect any other part of the image).
  • +channel – Specify that following operators only should should affect the color channels, and no longer modify the alpha channel. (This is the default, and therefore we need not provide it in the first, simpler example.)
  • -fx '#000' – Replace color channel contents with black pixels. (Because of +channel the alpha channel will not be affected).

It is quite important to include that final -fx option, otherwise all semi-transparent pixels in generated image will retain colors. (Since these pixels are half-transparent, it might not be obvious, but the end result is not what one expect.)

I found the list of ImageMagick options quite useful.

  • pretty good explanation to achieve my goal, when I am using second command that make my image more darker than actual grayscale image. Actually, my aim to achieve same color tone which is there in grayscale other than that transparency is as expected. any Idea to achieve that? – Hardik Patel Sep 18 '14 at 10:57
  • that's very useful, thanks! :) glad I didn't have to figure it out for myself – Sam Watkins May 16 '15 at 15:35
  • I love you!!!!!! – mescarra Jun 10 '16 at 12:49
  • Those kinds of comments are probably better to send to me through this channel: okcupid.com/profile/zrajm :) – zrajm Jun 13 '16 at 8:10
  • 1
    not sure why, but with the current version (ImageMagick 7.0.7-21) I only get solid white/black image as a result, without any alpha channel. I copied the command line verbatim, changing only input/output file paths. Maybe they made some backwards-incompatible changes. – Display Name Jan 30 '18 at 10:01

With ImageMagick 7.0, the above answer doesn't anymore. But you can use:

convert source.png -channel rgb -fx '#fff' result.png

Aka, fill the RGB channels with blank, and let the alpha as before.

  • I suspect you missed the '-alpha copy' :) – Chris Aug 12 '19 at 6:31

With imagemagick 7, i find it's necessary to specify -channel RGB before the -fx, otherwise you get a solid black image, like so:

convert input.png -alpha copy -channel RGB -fx '#000' out.png

And negate is useful if the you want black to be opaque and white to be transparent:

convert input.png -negate -alpha copy -channel RGB -fx '#000' out.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.