Page 1 of 1

Replacing all instances of one color with another

Posted: 2011-01-22T09:53:03-07:00
by saikyo
Hello community!

I'm one of those users who only jumps for Imagemagick a few times of year when I have a sticky situation I need to get out of...

So I have a directory full of .png files. One instance of a grey color in them is a little off, so I want to replace that color across the board with a new color.

The incorrect color is #C0C0C0 and the desired color is #BFBFBF.

I have tried a few things... but not quite getting the output I need.

Here is what I have tried:
convert * -fill #BFBFBF +opaque #C0C0C0
mogrify *.png -opaque "#C0C0C0" -fill "#BFBFBF"

I think I'm on the right track (been googling like crazy) but not quite there...

Any help greatly appreciated!

Re: Replacing all instances of one color with another

Posted: 2011-01-22T10:27:46-07:00
by fmw42
The following should work if you are replacing exact colors. If they are not exact, then you need to add -fuzz XX%

I recommand creating another directory to put the result, so you don't mess up the originals. So say the images are in tmp1 and you put the new ones in tmp2 (which you need to create ahead of time)

cd /fullpath2/tmp1
mogrify -path /fullpath2/tmp2 -format png -fill "#BFBFBF" -opaque "#C0C0C0" *.png

see http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/color_basics/#fuzz

The order of your command may be important, so try the above, which should be correct syntax.

Re: Replacing all instances of one color with another

Posted: 2011-01-22T16:08:32-07:00
by saikyo
Thanks so much! This worked like a charm :-)

I love internet peeples... ♡