Change channels strength

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
Dgandalf
Posts: 4
Joined: 2013-02-04T00:21:47-07:00
Authentication code: 6789

Change channels strength

Post by Dgandalf »

I would like to change image's colors, to take the strength of the Red color and split it between the other channels in the precentage I desire.

For example, if the RGB of a pixel in an image is as the following:
R:200
G:0
B:0

it will turn into (configurable):
R:0 (0%)
G:50 (25%)
B:150 (75%)

or even above the 100% into this result:
R: 50 (25%)
G: 50 (25%)
B: 100 (100%)

At first I tried using

Code: Select all

source.png -separate -swap 0,2 -combine result.png
but it only swaps the red channel with the blue.

Thanks in advance.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change channels strength

Post by snibgo »

"-color-matrix" will do this. See http://www.imagemagick.org/script/comma ... lor-matrix

For example, the following Windows command will increase the green channel by 25% of the red, and increase blue by 75% of the red, and zero the red.

Code: Select all

convert ^
  in.png ^
  -color-matrix ^
  ^" 0.00 0.00 0.00 0.00 0.00 0.00 ^
     0.25 1.00 0.00 0.00 0.00 0.00 ^
     0.75 0.00 1.00 0.00 0.00 0.00 ^
     0.00 0.00 0.00 1.00 0.00 0.00 ^
     0.00 0.00 0.00 0.00 1.00 0.00 ^
     0.00 0.00 0.00 0.00 0.00 1.00 ^" ^
  out.png
snibgo's IM pages: im.snibgo.com
Dgandalf
Posts: 4
Joined: 2013-02-04T00:21:47-07:00
Authentication code: 6789

Re: Change channels strength

Post by Dgandalf »

Thanks, I've found a good documentation here:

Unfortunatly I cann't seem to achieve my goal. I would like to turn the red in this picture into blue one (not pure blue), blue like this one (ignore the pattern):
Image

without the white color being changed to other color (since its 255/255/255 and the red color is being lowred)

Image

You think its possible to achieve it without coloring the white-gray area? If yes, an explanation / simple example will be handful.

Thanks.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change channels strength

Post by snibgo »

Ah, that's easy.

Code: Select all

convert red_bg.jpg -modulate 100,100,20.0976 blue_bg.png
Where did "20.0976" come from?

Code: Select all

convert large_0266443.jpg -resize 1x1! -colorspace HSL txt:
# ImageMagick pixel enumeration: 1,1,255,hsl
0,0: (153,247, 21)  #99F715  hsl(59.8901%,96.9818%,8.39399%)

convert red_bg.jpg -crop 20x20+402+123 -resize 1x1! -colorspace HSL txt:
# ImageMagick pixel enumeration: 1,1,255,hsl
0,0: (255,213, 85)  #FFD555  hsl(99.8413%,83.5035%,33.4463%)
The blue background has an average hue of 59.8901%. A small patch of the red is hue=99.8413%. (Examination with the Gimp eyedropper tells me all the reds have the same hue, and the greys really are grey.) We want a change of:

99.8413 - 59.8901 = 39.9512%, or 0.399512 as a fraction of one.

For the modulate command, a delta hue of 200 is a "full circle", 360 degrees. So we want a delta hue of 0.399512 * 200. But the base isn't zero, it is 100. (Give it a value of 100 or 300 and we get no change in hue. 0 and 200 gives us 180 degree rotation of hue.)

100 - 0.399512 * 200 = 20.0976. QED.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Change channels strength

Post by anthony »

modulate would be the solution, though perhaps using HCL colorspace so as to not brighten or darken the image with the change in hue (as you get in HSL or HSB colorspaces).

See Im examples, Color Modifications, Modulate in HCL Colorspace
http://www.imagemagick.org/Usage/color_ ... dulate_HCL

See also the HCL color wheel (compare with the HSL wheel in examples above)
http://www.imagemagick.org/Usage/color_ ... rwheel_HCL
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Dgandalf
Posts: 4
Joined: 2013-02-04T00:21:47-07:00
Authentication code: 6789

Re: Change channels strength

Post by Dgandalf »

Works like a magic!

Thanks for the detailed explanations, I will read how to do these magics by myself now :)
Dgandalf
Posts: 4
Joined: 2013-02-04T00:21:47-07:00
Authentication code: 6789

Re: Change channels strength

Post by Dgandalf »

There is an option to use the same command

Code: Select all

convert red_bg.jpg -modulate 100,100,20.0976 blue_bg.png
but instead of giving it an image as source and get image as output, to give it a hexadecimal value and get the output hexadecimal value?

Thanks for the help guys, You are real experts :D
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change channels strength

Post by snibgo »

Yup.

Code: Select all

D:\web\im>"%IMG%convert" xc:#AABBCC txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (43690,48059,52428)  #AAAABBBBCCCC  srgb(170,187,204)

D:\web\im>"%IMG%convert" xc:#AABBCC -modulate 100,100,20.9076 txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (51316,52428,43690)  #C874CCCCAAAA  srgb(78.3032%,80%,66.6667%)
These commands create an image of default size, which is 1x1 pixel, and outputs it in text format.
snibgo's IM pages: im.snibgo.com
Post Reply