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
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
see
http://www.imagemagick.org/Usage/masking/#alpha_shape
http://www.imagemagick.org/Usage/maskin ... background
Convert grayscale image to black plus transparency?
Re: Convert grayscale image to black plus transparency?
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:
To instead make the white pixels transparent and keep the black as they are, use:
Let's explain that last command a bit more thoroughly:
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
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
Code: Select all
convert source.png -alpha copy -channel alpha -negate +channel -fx '#000' result.png
- – Is the ImageMagic command (one of several)
Code: Select all
convert
- – The greyscale source image.
Code: Select all
source.png
- – Copy contents of the previous file into the alpha channel.
Code: Select all
-alpha copy
- – Specify that following operators only should affect the alpha channel.
Code: Select all
-channel alpha
- – Invert the alpha channel (will, because of the previous
Code: Select all
-negate
not affect any other part of the image).Code: Select all
-chanel alpha
- – 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
+channel
- – Replace color channel contents with black pixels. (Because of
Code: Select all
-fx '#000'
the alpha channel will not be affected).Code: Select all
+channel
Code: Select all
-fx
I found the list of ImageMagick options quite useful: http://www.imagemagick.org/script/comma ... ptions.php