Replacing all instances of one color with another

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
saikyo
Posts: 2
Joined: 2011-01-22T09:31:18-07:00
Authentication code: 8675308

Replacing all instances of one color with another

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

Re: Replacing all instances of one color with another

Post 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.
saikyo
Posts: 2
Joined: 2011-01-22T09:31:18-07:00
Authentication code: 8675308

Re: Replacing all instances of one color with another

Post by saikyo »

Thanks so much! This worked like a charm :-)

I love internet peeples... ♡
Post Reply