Question

I have a grey scale image, from which I wish to convert all grey pixels into half transparent pixels, and white ones into transparent pixels.

How could I process a grey scale raster image via shell ?

Input : enter image description here

Output (here made via Gimp): enter image description here


Current Gimp process via GUI:

  1. GIMP 2.6 > Load your shaded relief image (....shaded.tif : are grayscale) or a screenshoot of your shaded relief (screenshot : RGB colors)
  2. Force it to be RGB: Gimp > Image > Mode > RGB, click.
  3. Delete the grey : Colors > "color to alpha" pop up > uncheck "preview", click on the horizontal color rectangle > "Color to alpha color picker" pop up> bottom right corner, click on the icon eyes dropper > choice you color of to delete (some grey pixel in a flat plain) > validate.
  4. Delete an other color (white, black background) > same.
  5. File > save as > ProjectName_relief_whitened.png (to keep transparency)
Was it helpful?

Solution

[note: SO images display, and image background CSS makes hard to see the subtile differences between files.]

Given the following gray scale input.png :

enter image description here

1a. To make black pixels of this image transparent and linearly keep the white pixels as they are, run this command:

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

1b. To make white pixels transparent and linearly keep the black as they are, use:

convert source.png -alpha copy -channel alpha -negate +channel result.png

Manual:

  • convert – is the ImageMagic command (one of several)
  • source.png – is the greyscale source image.
  • -alpha copy – it copy contents of the previous file into the alpha channel.
  • -channel alpha – it specify that following operators only should affect the alpha channel.
  • -negate – it invert the current channel (channel alpha).
  • +channel – Specify that following operators only affect the opposite channel. For us, it switch focus from the alpha channel, to the color channel. (color channel is initially the default)
  • -fx '#000' – Replace current channel (for us, the color channel) contents with black pixels, so the end result actually fully depends on the alpha channel. If not included, all semi-transparent pixels in generated image will retain colors, from #FFF (white) to #000 (black).

Result of 1b:

enter image description here

Wiping out plains: An additional processing could wipe out most of the flat plains, which appears around greys (#DDDDDD) with opacity:~50%. This could be done by :

convert input.png -fuzz 8% -transparent "#DDDDDD" grey_no.8pc.png
convert grey_no.8pc.png -alpha copy -channel alpha -negate +channel result.grey_no.png

so the plains avoid an useless #DDDDDD, opacity:50% overlay.

enter image description here

See also:

  1. ImageMagick options: http://www.imagemagick.org/script/command-line-options.php
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top