Changing RGB values of all pixels in an image

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
Giant Speck
Posts: 3
Joined: 2010-08-15T02:29:25-07:00
Authentication code: 8675308

Changing RGB values of all pixels in an image

Post by Giant Speck »

Hello, all.

First of all, I want to make it clear that I have tried searching for this not only on Google but on these forums, but I haven't been able to find a solution. I apologize if this topic has already been brought up.

Essentially, I want to take an image and shift the RGB values of every pixel within that image. For example, I am recoloring a bright blue image. I want to shift the RGB values of every pixel by the following amounts in order to achieve a brownish image:

R: -74
G: -109
B: -142

A pixel with a RGB value of (128,160,190) becomes (54,51,58), for example.

Is there any way to do this using ImageMagick's command-line interface? My only option right now seems to be to manually change the values in GIMP which has proven to be a painstakingly slow and arduous process.
markmarques
Posts: 88
Joined: 2010-06-29T14:36:09-07:00
Authentication code: 8675308

Re: Changing RGB values of all pixels in an image

Post by markmarques »

Hi ...
I suppose that using the "-color-matrix" option in convert would be your answer to your problem...
As the web usage docs referer it it could e a way to solve your problem ...

Something in like :
convert -color-matrix " 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0" imagein imageout

Although you would need to calculate the "correct values" for the matrix in order to achieve what you need ...
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Changing RGB values of all pixels in an image

Post by Drarakel »

One possibility is to use "-evaluate". But I think, you have to adjust the subtraction values to your QuantumRange here (e.g. by multiplying them with 257 for a Q16 version). Example:
convert input -channel R -evaluate subtract 19018 -channel G -evaluate subtract 28013 -channel B -evaluate subtract 36494 output
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Changing RGB values of all pixels in an image

Post by fmw42 »

-color-matrix has offsets

see

http://www.imagemagick.org/script/comma ... lor-matrix

also see http://www.adobe.com/devnet/flash/artic ... ns_04.html

but set the offsets to fractions (-79/255=-.310, -109/255=-.427, -142/255=-.557)

convert rose: -color-matrix \
"1 0 0 0, 0, -0.310 \
0 1 0 0, 0, -0.427 \
0 0 1 0, 0, -0.557 \
0 0 0 1, 0, 0 \
0 0 0 0, 1, 0 \
0 0 0 0, 0, 1" \
rose_adjusted.png
Giant Speck
Posts: 3
Joined: 2010-08-15T02:29:25-07:00
Authentication code: 8675308

Re: Changing RGB values of all pixels in an image

Post by Giant Speck »

Drarakel wrote:One possibility is to use "-evaluate". But I think, you have to adjust the subtraction values to your QuantumRange here (e.g. by multiplying them with 257 for a Q16 version). Example:
convert input -channel R -evaluate subtract 19018 -channel G -evaluate subtract 28013 -channel B -evaluate subtract 36494 output
This solution produced an outcome closest to what I was trying to achieve. I compared the ImageMagick-produced image side-by-side with an image that I recolored manually in GIMP, and the RGB values were almost 100% spot-on.

When I tried to produce the same result with -color-matrix, the saturation and hue were off. The results were not accurate enough.

Thank you to all that helped me!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Changing RGB values of all pixels in an image

Post by fmw42 »

It is likely I made an error in my calculations of the fractions as the following all give exactly the same results:



convert rose: -channel R -evaluate subtract 19018 \
-channel G -evaluate subtract 28013 \
-channel B -evaluate subtract 36494 \
rose_evaluate1.png


rval=`convert xc: -format "%[fx:74*100/255]" info:`
gval=`convert xc: -format "%[fx:109*100/255]" info:`
bval=`convert xc: -format "%[fx:142*100/255]" info:`
convert rose: -channel R -evaluate subtract $rval% \
-channel G -evaluate subtract $gval% \
-channel B -evaluate subtract $bval% \
rose_evaluate2.png


rval=`convert xc: -format "%[fx:-74/255]" info:`
gval=`convert xc: -format "%[fx:-109/255]" info:`
bval=`convert xc: -format "%[fx:-142/255]" info:`
convert rose: -color-matrix \
"1 0 0 0, 0, $rval \
0 1 0 0, 0, $gval \
0 0 1 0, 0, $bval \
0 0 0 1, 0, 0 \
0 0 0 0, 1, 0 \
0 0 0 0, 0, 1" \
rose_colormatrix.png

compare -metric rmse rose_evaluate1.png rose_colormatrix.png null:
0 (0)

compare -metric rmse rose_evaluate2.png rose_colormatrix.png null:
0 (0)
Giant Speck
Posts: 3
Joined: 2010-08-15T02:29:25-07:00
Authentication code: 8675308

Re: Changing RGB values of all pixels in an image

Post by Giant Speck »

Well, that's equally interesting. Thank you for that.

I think I'm going to stick with the "-evaluate subtract" method, though, as it involves far less typing and leaves far less room for my n00biness to make errors. :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Changing RGB values of all pixels in an image

Post by anthony »

If you are going to resort to FX why not just subtract the color!

Code: Select all

convert rose: -fx 'u-rgb(74,109,142)'  rose_result.png
NOTE -evaulate uses Actual values are not normalized values for subtract (-function uses normalized)

Assuming a 8 bit IM

convert rose: -channel R -evaluate subtract 74 \
-channel G -evaluate subtract 109 \
-channel B -evaluate subtract 142 \

For IM Q16 you will need to use a value of value*65535/255

Of course use a percentage value then it is value*100/255 just as Fred specified above for ALL versions of IM.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply