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.
Changing RGB values of all pixels in an image
-
- Posts: 3
- Joined: 2010-08-15T02:29:25-07:00
- Authentication code: 8675308
-
- Posts: 88
- Joined: 2010-06-29T14:36:09-07:00
- Authentication code: 8675308
Re: Changing RGB values of all pixels in an image
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 ...
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
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
convert input -channel R -evaluate subtract 19018 -channel G -evaluate subtract 28013 -channel B -evaluate subtract 36494 output
- 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
-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
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
-
- Posts: 3
- Joined: 2010-08-15T02:29:25-07:00
- Authentication code: 8675308
Re: Changing RGB values of all pixels in an image
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.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
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!
- 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
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)
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)
-
- Posts: 3
- Joined: 2010-08-15T02:29:25-07:00
- Authentication code: 8675308
Re: Changing RGB values of all pixels in an image
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.
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.

- 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
If you are going to resort to FX why not just subtract the color!
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.
Code: Select all
convert rose: -fx 'u-rgb(74,109,142)' rose_result.png
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/
https://imagemagick.org/Usage/