Page 1 of 1
Changing RGB values of all pixels in an image
Posted: 2010-08-15T02:42:28-07:00
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.
Re: Changing RGB values of all pixels in an image
Posted: 2010-08-15T03:21:58-07:00
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 ...
Re: Changing RGB values of all pixels in an image
Posted: 2010-08-15T04:42:49-07:00
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
Re: Changing RGB values of all pixels in an image
Posted: 2010-08-15T12:07:40-07:00
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
Re: Changing RGB values of all pixels in an image
Posted: 2010-08-15T17:16:53-07:00
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!
Re: Changing RGB values of all pixels in an image
Posted: 2010-08-15T17:37:55-07:00
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)
Re: Changing RGB values of all pixels in an image
Posted: 2010-08-15T19:25:39-07:00
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.

Re: Changing RGB values of all pixels in an image
Posted: 2010-08-15T21:40:47-07:00
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.