89

How do I invert the colors of an image using the ImageMagick convert tool, so that black becomes white and white becomes black?

In other words, I want to turn this:

original

into this:

inverted

3 Answers 3

134

Use the -negate option:

convert input.png -channel RGB -negate output.png

The -negate option replaces each pixel with its complementary color. The -channel RGB option is necessary as of ImageMagick 7 to prevent the alpha channel (if present) from being negated. (Thanks to goto-bus-stop for this insight.)

See also the documentation for -negate.

5
  • 6
    this made my black and white png completely transparent...
    – minseong
    Jan 21, 2018 at 15:49
  • This worked for me
    – Shadi
    Mar 6, 2018 at 4:53
  • 9
    The transparency problem is because the alpha channel is also inverted. Use -channel RGB -negate to avoid that. Oct 7, 2018 at 13:48
  • 1
    That's interesting. The ImageMagick on my system (version 6.8.9) only negates the RGB channels, and not the alpha channel by default, so -channel RGB isn't necessary. It only inverts the alpha channel if I pass -channel RGBA -negate. I'm not sure what explains the different behavior.
    – Jo Liss
    Oct 18, 2018 at 15:57
  • It works for me, without negate. What I'm curious about though is: while identify sees the input as "8-bit sRGB", the output is "8-bit Grayscale Gray".
    – user74094
    Nov 26, 2018 at 0:25
12

ImageMagick 6.x can invert color intensities with -negate option only. ref) https://www.imagemagick.org/script/command-line-options.php#negate

convert input.png -negate output.png

ImageMagick 7.x requires the -channel option with -negate. ref) https://imagemagick.org/script/porting.php#cli Changed Options

convert input.png -channel RGB -negate output.png

This is because the default active channels contain transparency(opaque/alpha) in ImageMagick 7.x

ImageMagick6: DefaultChannels = ((AllChannels | SyncChannels) &~ OpacityChannel)
ImageMagick7: DefaultChannels = AllChannels

Most algorithms update the red, green, blue, black (for CMYK), and alpha channels. Usability of -negate seems to be sacrificed for overall consistency.

More detail for you. http://blog.awm.jp/2018/11/18/im7negate/

4
  • 2
    convert input.png -negate output.png works fine for me on ImageMagick 7.0.8 (ArchLinux)
    – msrd0
    Nov 23, 2018 at 16:38
  • There is no problem on RGB image without transparency. RGBA with transparency image cause problem, only ImageMagick7.
    – yoya
    Nov 28, 2018 at 5:42
  • You can check PNG file has transparency or not. identify -verbose input.png | grep Type It ouput TrueColor or TrueColorAlpha.
    – yoya
    Nov 28, 2018 at 5:48
  • Great answer, thanks! I went ahead and added -channel RGB to my answer.
    – Jo Liss
    Nov 29, 2018 at 18:06
0

For powershell (Windows OS) need use:

magick input.png -channel RGB -negate output.png

Note. I using it in imagemagick 7.1.1-18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .