Simplest way to invert image hue.

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
coolperez8
Posts: 27
Joined: 2016-03-11T07:27:11-07:00
Authentication code: 1151

Simplest way to invert image hue.

Post by coolperez8 »

So, let's say I have an image, and I want to invert the hue, but not the lightness. I know this command will produce the same result, but I'm looking for a simpler command.

Code: Select all

convert "input.png" -negate -colorspace Lab ^
          -channel R   -negate   +channel ^
          -colorspace sRGB "hue_invert.png"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Simplest way to invert image hue.

Post by fmw42 »

Create test image:

Code: Select all

convert -size 100x100 xc:red xc:green1 xc:blue +append rgb.png
Image

There is no "hue" in LAB colorspace. You need to use HCL, HSL, or HSV etc to affect the hue directly. But this does not give what you might expect and you will get different results from each of the H-based colorspaces. This is due to the fact that the hue is cyclical, so inverting the hue values is not the same as rotating by 180 degrees.

Code: Select all

convert rgb.png -colorspace HCL -channel R -negate +channel -colorspace sRGB rgb_invert1.png
Image

In fact, HSV and HSL will just swap the green and blue channels.

Code: Select all

convert rgb.png -colorspace HSV -channel R -negate +channel -colorspace sRGB rgb_invert0.png
Image


What I think you want is to rotate the hue by 180 (in -modulate hue would go from 100 to 0 or 200 to be equivalent to -+180 deg). This converts primary colors to secondary colors.

Code: Select all

convert rgb.png -modulate 100,100,0 rgb_invert2.png
Image

But this is no different from:

Code: Select all

convert rgb.png -negate rgb_invert2.png
Image

And is the same as adding 50% modulo to the hue channel

Code: Select all

convert rgb.png -colorspace HSV -channel r -evaluate AddModulus 50% +channel -colorspace sRGB rgb_invert4.png
Image


You get a slightly different result by negating the A and B channels of LAB:

Code: Select all

convert rgb.png -colorspace LAB -channel GB -negate +channel -colorspace sRGB rgb_invert3.png
Image

So it really depends upon what you mean by hue and which colorspace you work in.
Post Reply