Page 1 of 1

replace color in images

Posted: 2011-03-01T09:29:15-07:00
by ratneshsoni
i want to replace the color in image with another color in the image.
Like i want to replace white color with another color like red.
please help me in solving this problem....

Re: replace color in images

Posted: 2011-03-01T10:39:11-07:00
by fmw42
convert image -fuzz XX% -fill red -opaque white result

where -fuzz XX% allows the match to white to get colors within XX% of white. If you want a perfect match remove the -fuzz XX% or set XX% to 0


see

http://www.imagemagick.org/Usage/color_basics/#replace

see color definitions at http://www.imagemagick.org/script/color.php

Re: replace color in images

Posted: 2011-03-01T18:52:27-07:00
by anthony
The bigger question... Is it just red, or all shades of red to shades of some other color?
That is a much harder problem and may need a look at a small example of the image you are processing.

Re: replace color in images

Posted: 2011-11-09T08:53:57-07:00
by frisket
I have a similar problem. The PNG images generated by ditaa have an invariable background and margin of rgb(254,254,254) which I want to crop off. The tools I have tried (converting to PDF and using pdfcrop, or converting to PNM and using pnmcrop) fail because the background and margin are not rgb{255,255,255). I have tried to convert the pixels, but this fails:

Code: Select all

convert $NAME.png -fuzz 0% -fill 'rgb(254,254,254)' -opaque 'rgb(255,255,255)' foo.png
I am testing the result by opening the output in GIMP and using the eyedropper to pick the top right pixel: it remains rgb(254,254,254).
I have obviously misunderstood how to do this: does anyone have the necessary incantation?

I then want to crop off all margin: is there an argument to -crop that will simply remove all margin in the background color, in the same way as pdfcrop or pnmcrop do, without having to measure each image and specify how much to crop off?

Re: replace color in images

Posted: 2011-11-09T09:42:57-07:00
by Bonzo
A sample image would help and you can use -trim to remove a border.

Code: Select all

convert $NAME.png -trim  foo.png

Re: replace color in images

Posted: 2011-11-09T10:34:42-07:00
by fmw42
convert $NAME.png -fuzz 0% -fill 'rgb(254,254,254)' -opaque 'rgb(255,255,255)' foo.png
If your background is not a constant value, then increase the fuzz value. But if you just want to crop off the border, then as Bonzo suggests, use

convert $NAME.png -fuzz X% -trim +repage foo.png

The +repage is needed to clear the larger virtual canvas. Choose X% as small a possible such that it trims the way you want. 0 may not be enough as that requires a perfectly constant background color.

Re: replace color in images

Posted: 2015-12-24T06:03:56-07:00
by kadtanatul
Unable to replace red color with green which is not exactly red i.e. shade of red ( #484848 )Image

Re: replace color in images

Posted: 2015-12-24T06:06:25-07:00
by kadtanatul

Re: replace color in images

Posted: 2015-12-24T10:03:00-07:00
by fmw42
Your red color is #C62529. (#484848 is gray)

This works for me:

Code: Select all

convert 15nnfhk.jpg.png -fuzz 10% -fill green1 -opaque "#C62529" result.png
Why does your image have two suffixes .jpg and .png?

Re: replace color in images

Posted: 2015-12-24T14:11:06-07:00
by glennrp
frisket wrote:I have a similar problem. The PNG images generated by ditaa have an invariable background and margin of rgb(254,254,254) which I want to crop off. The tools I have tried (converting to PDF and using pdfcrop, or converting to PNM and using pnmcrop) fail because the background and margin are not rgb{255,255,255). I have tried to convert the pixels, but this fails:

Code: Select all

convert $NAME.png -fuzz 0% -fill 'rgb(254,254,254)' -opaque 'rgb(255,255,255)' foo.png
I am testing the result by opening the output in GIMP and using the eyedropper to pick the top right pixel: it remains rgb(254,254,254).
I have obviously misunderstood how to do this: does anyone have the necessary incantation?

I then want to crop off all margin: is there an argument to -crop that will simply remove all margin in the background color, in the same way as pdfcrop or pnmcrop do, without having to measure each image and specify how much to crop off?
It looks to me as though you've got it backwards. Your command changes all pure white pixels to 254,254,254.