I want to do - seemingly - simply image processing. I want to replace all blue pixels with white pixels. The obvious command would be:

enter image description here

ColorReplace[image2, Blue -> White]

Instead of getting the desired result, i.e. the picture, in which the blue line is changed to white, I get the following:

ColorReplace[image2, RGBColor[0, 0, 1] -> GrayLevel[1]]

Any ideas?


1 Answer 1


Do you see an error message in the Messages window? When this happens (the result of an evaluation is more or less the expression you entered), it's usually because it can't be evaluated, e.g. because image2 is not actually an image. If I Import the image from your post, ColorReplace accepts it as an argument, but since the blue you're trying to replace isn't exactly Blue, it doesn't replace anything.

You can use a trick from the ColorReplace documentation, and look for the closest "bluish" color that's actually in the image, and replace that:

image2 = Import["https://i.stack.imgur.com/lGVPj.png"];

g = Nearest[DominantColors[image2, 10], Blue, 
  DistanceFunction -> ColorDistance]
ColorReplace[image2, g[[1]] -> White]

enter image description here,

This mostly works, except that I guess anti aliasing makes parts of the blue line darker than ColorReplace's threshold. You can play with the threshold (that last parameter), but I think if you only want to make blue pixels white, the easiest way would be to just use the blue channel:


enter image description here

  • $\begingroup$ I don't see an error message when I execute the command. I thought that the option "Blue->White" in the ColorReplace command already takes all "blueish" colors. I also tried the ColorReplace by exactly stating the blue color: ColorReplace[image2,RGBColor[0.0196,0.5725,0.8314]->RGBColor[1,1,1]], but that also didn't work. Anyway: Thanks a lot! $\endgroup$
    – Niki
    Jul 31, 2015 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.