Search code examples
imagemagickimage-manipulationgraphicsmagicknode-imagemagick

GraphicksMagic: white -> transparent


I found many forum posts about this but none of what I found could solve it for me:

I need to convert the white background of images like the one attached to transparent using GraphicsMagick (or ImageMagick). But when I run any of the below commands, the background becomes black instead of transparent.

In GraphicsMagick I tried:

gm convert input.png -opaque white output.png
gm convert input.png -fill transparent -opaque white output.png
gm convert input.png -background transparent -opaque white output.png
gm convert input.png -background transparent -fill transparent -opaque white output.png

enter image description here

My application is in nodeJS so a nodeJS solution using the GM package would be even better;

gm('input.png')
    .dosomething()
    .write('output.png');

Solution

  • The image does not already have an alpha channel, so use -matte:

    gm convert input.png -matte -fill transparent -opaque white output.png