Page 1 of 1
Replace each RGB value with another number
Posted: 2013-10-02T18:45:46-07:00
by nftprd
Hello guys, this might be easy but I'm new here so I appreciate any help I can get.
I have a color table that has custom (255) values for red, green, and blue like this:
Code: Select all
red = [ 0, 2, 3, 5 ...
green = [ 0, 1, 1, 2 ...
blue = [ 50, 50, 50, 70 ...
Basically I would like to replace each rgb value from an image with my custom value..
For example,
Code: Select all
rgb(1,50,100) gets replaced by rgb(2,50,100) because red[1] = "2"
rgb(0, 0, 0) gets replaced by rgb(0, 0, 50) because blue[0] = "50" etc...
I've been able to implement this in PHP and Actionscript quite easily but I have to iterate through each pixel and replace each value, which makes it slow.
Does ImageMagick have an easy way to do this? I can easily change the formatting of my table to allow for the replacement.
Thanks!
Re: Replace each RGB value with another number
Posted: 2013-10-02T19:04:55-07:00
by fmw42
Convert your table of colors into a 1D image and use -clut to apply that to your image.
see
http://www.imagemagick.org/Usage/color_mods/#color_lut
You can either append 256 1pixel images together to create the 1D image or use NetPBM format to convert text color values to an image.
convert xc:"rgb(0,0,0)" xc:"rgb(...)" ... +append 1Dlut.png
see
http://www.imagemagick.org/Usage/formats/#netpbm
You would skip the -scale and would create 3 lists of 256 grayvalues, one for each of the r,g,b channels then combine them together and apply to the image.
also
http://netpbm.sourceforge.net/doc/pgm.html will explain the format.
Make 3 lists (or arrays): redlist, greenlist, bluelist
echo "P2 256 1 255 $redlist" | convert - redlut.png
echo "P2 256 1 255 $greenlist" | convert - greenlut.png
echo "P2 256 1 255 $bluelist" | convert - bluelut.png
convert redlut.png greenlut.png bluelut.png -combine -colorspace sRGB colorlut.png
convert image colorlut.png -clut resultimage
P.S. What version of IM are you using and on what platform? Syntax may differ!
Re: Replace each RGB value with another number
Posted: 2013-10-02T19:45:17-07:00
by nftprd
I'm using 6.7.7-10 on Linux. I'm following your directions, I'll update you once I've got it. Thanks so much!
Re: Replace each RGB value with another number
Posted: 2013-10-02T20:15:10-07:00
by nftprd
Thank you so much, Fred!! It works perfectly.
I've put the commands in a neat PHP function if anyone else needs it to create a color LUT image...
Code: Select all
function table2image($map, $name='colorlut'){
$r = 'echo "P2 256 1 255 '.implode(' ',$map['red']). '" | convert - rlut.png';
$g = 'echo "P2 256 1 255 '.implode(' ',$map['green']).'" | convert - glut.png';
$b = 'echo "P2 256 1 255 '.implode(' ',$map['blue']). '" | convert - blut.png';
exec("$r;$g;$b");
exec("convert rlut.png glut.png blut.png -combine -colorspace sRGB $name.png");
unlink('rlut.png');
unlink('glut.png');
unlink('blut.png');
}
table2image($map, 'name');
Re: Replace each RGB value with another number
Posted: 2013-10-02T20:21:41-07:00
by fmw42
The other way would have to been to make a text file of
xc:"rgb(...)"
xc:"rgb(...)"
.
.
.
Then just do
convert @filename.txt +append colorlut.png
Then
convert image colorlut.png -clut resultimage