Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.)

share|improve this question
up vote 14 down vote accepted

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.

share|improve this answer
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 at 12:49
Those kinds of comments are probably better to send to me through this channel: :) – zrajm Jun 13 at 8:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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