Replace each RGB value with another number

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
nftprd
Posts: 3
Joined: 2013-10-02T18:33:20-07:00
Authentication code: 6789

Replace each RGB value with another number

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace each RGB value with another number

Post 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!
nftprd
Posts: 3
Joined: 2013-10-02T18:33:20-07:00
Authentication code: 6789

Re: Replace each RGB value with another number

Post 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!
nftprd
Posts: 3
Joined: 2013-10-02T18:33:20-07:00
Authentication code: 6789

Re: Replace each RGB value with another number

Post 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');
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace each RGB value with another number

Post 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
Post Reply