43

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

66

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.

| improve this answer | |
  • 5
    this made my black and white png completely transparent... – theonlygusti Jan 21 '18 at 15:49
  • This worked for me – shadi Mar 6 '18 at 4:53
  • 3
    The transparency problem is because the alpha channel is also inverted. Use -channel RGB -negate to avoid that. – goto-bus-stop Oct 7 '18 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 '18 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 '18 at 0:25
4

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/

| improve this answer | |
  • 1
    convert input.png -negate output.png works fine for me on ImageMagick 7.0.8 (ArchLinux) – msrd0 Nov 23 '18 at 16:38
  • There is no problem on RGB image without transparency. RGBA with transparency image cause problem, only ImageMagick7. – yoya Nov 28 '18 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 '18 at 5:48
  • Great answer, thanks! I went ahead and added -channel RGB to my answer. – Jo Liss Nov 29 '18 at 18:06
0

My Canon Pixma printer's black ink has been depleted and I cannot print my QR Code which is in black and white color. You cannot color invert this because when black becomes white and white becomes black, the boundary perimeter of the square blends on the white bond paper and the QR code will not scan. This is part of the QR code specification with regards to keeping the boundary square visually discernable.

Therefore, I needed to change every black color into blue using imagemagick convert command:

convert qrdata89.bmp -fuzz 20% -fill blue -opaque black qrdata89_blue.bmp

Problem solved.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.