Convert grayscale image to black plus transparency?

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

Re: Convert grayscale image to black plus transparency?

Post by fmw42 »

So this should be what is wanted:


convert sample-gray.jpeg -set colorspace gray \
\( -clone 0 -fill black -colorize 100% \) \
\( -clone 0 -negate \) \
-delete 0 -alpha off -compose copy_opacity -composite PNG32:sample_gray_correct.png

Image



This also works and is shorter:

convert sample-gray.jpeg -set colorspace gray \
-negate -background black -alpha shape -background black -alpha background \
PNG32:sample_gray_correct2.png

Image

see
http://www.imagemagick.org/Usage/masking/#alpha_shape
http://www.imagemagick.org/Usage/maskin ... background
zrajm
Posts: 1
Joined: 2012-12-27T11:30:21-07:00
Authentication code: 6789

Re: Convert grayscale image to black plus transparency?

Post by zrajm »

I had the exact same problem, and after much googling and reading of the ImageMagick documentation, this is what I've found.

To make the make the black pixels of an image transparent and keep the white pixels as they are, run this command:

Code: Select all

convert source.png -alpha copy -fx '#fff' result.png
To instead make the white pixels transparent and keep the black as they are, use:

Code: Select all

convert source.png -alpha copy -channel alpha -negate +channel -fx '#000' result.png
Let's explain that last command a bit more thoroughly:
  • Code: Select all

    convert
    – Is the ImageMagic command (one of several)
  • Code: Select all

    source.png
    – The greyscale source image.
  • Code: Select all

    -alpha copy
    – Copy contents of the previous file into the alpha channel.
  • Code: Select all

    -channel alpha
    – Specify that following operators only should affect the alpha channel.
  • Code: Select all

    -negate
    – Invert the alpha channel (will, because of the previous

    Code: Select all

    -chanel alpha
    not affect any other part of the image).
  • Code: Select all

    +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.)
  • Code: Select all

    -fx '#000'
    – Replace color channel contents with black pixels. (Because of

    Code: Select all

    +channel
    the alpha channel will not be affected).
It is quite important to include that final

Code: Select all

-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: http://www.imagemagick.org/script/comma ... ptions.php
Post Reply