Invert colors with ImageMagick

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


Solution 1:

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 @yoya for this insight!)

See also the documentation for -negate.

Solution 2:

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/