Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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 an image with a black background with white elements in the foreground. The image has smoothing, so there are 'in-between' pixels that aren't strictly black or white. I'd like to make the background transparent. I found several links on how to do this with imagemagick, but all of them strictly replace one color (say #000000FF" with full alpha "#00000000"). I want to preserve blending... so if there's a gray pixel "#777777FF", it'd get transformed into something like "#FFFFFF77", or whatever would make sense.

Does anyone know how to do this?

share|improve this question

This article explains very well how to do this:

I wrote a small article about it on my blog:

Basically, what you want to do is create a mask, and then apply the mask to the image. You can do this in two steps:

$ convert image.png -colorspace HSB -separate -negate image_mask.png
$ convert image.png -alpha Off \
>    image_mask.png -compose CopyOpacity -composite \
>    PNG32:image_alpha.png

This will make white the transparent colour. To make black the transparency colour remove the -negate flag.

share|improve this answer

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.