replace color in images

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
ratneshsoni
Posts: 38
Joined: 2010-12-14T03:56:10-07:00
Authentication code: 8675308

replace color in images

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

Re: replace color in images

Post 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
Last edited by fmw42 on 2011-03-01T20:05:38-07:00, edited 2 times in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: replace color in images

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
frisket
Posts: 1
Joined: 2011-11-09T08:39:40-07:00
Authentication code: 8675308

Re: replace color in images

Post 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?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: replace color in images

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

Re: replace color in images

Post 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.
kadtanatul
Posts: 2
Joined: 2015-12-24T05:48:54-07:00
Authentication code: 1151

Re: replace color in images

Post by kadtanatul »

Unable to replace red color with green which is not exactly red i.e. shade of red ( #484848 )Image
kadtanatul
Posts: 2
Joined: 2015-12-24T05:48:54-07:00
Authentication code: 1151

Re: replace color in images

Post by kadtanatul »

User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: replace color in images

Post 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?
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: replace color in images

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