Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I hope this isn't too trivial a question, but how can one quickly convert pixels in an image with one value, e.g. {r,g,b} = {0.0000,0.0000,0.0000}, to another, e.g. {r,g,b} = {0.5000,0.5000,0.5000}? The closest built-in function I could find to do this, ColorReplace, seems to require me to specify an actual color like Black in order to execute the change. For example:

ColorReplace[image, {0, 0, 0}, {0.5, 0.5, 0.5}]

...fails and gives the error message:

ColorReplace::color: Expecting an image, graphics, a color, or a color 
replacement rule instead of {0,0,0}.

Various other, similar, things I've tried have also failed. Even if I use ColorSeperate to attempt this on a grayscale image, I can't seem to get anything to work (outside of explicitly generating and correcting the ImageData output).

share|improve this question

closed as off-topic by cormullion, m_goldberg, Artes, Kuba, rm -rf Aug 6 '13 at 16:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – cormullion, m_goldberg, Artes, Kuba, rm -rf
If this question can be reworded to fit the rules in the help center, please edit the question.

You should put there colors not lists, so RGBColor[{0,0,0}]. –  Kuba Aug 6 '13 at 13:16
The replacement should also be a Rule (->), such as Black -> Gray in your example. –  Michael E2 Aug 6 '13 at 13:29

3 Answers 3

up vote 2 down vote accepted

ImageValue reads the value of a pixel from any location in an image and ReplaceImageValue sets it to whatever you desire. The other way to proceed is to use ImageData and turn the image into a matrix of values which can be set using any of the normal list operations. To find all the pixels with a given value (for instance, all those of a particular color), you can use ImageValuePositions.

share|improve this answer
With regards to ReplaceImageValue, I don't ab initio know the positions of the pixels I want to swap from one color to another. I'm looking for a way to swap one pixel value to another conditioned on its initial value (here, that it has {r,g,b} = {0,0,0}). I can scan through the ImageData output, but this is a rather slow process. –  Sparse Pine Aug 6 '13 at 13:13
See update... does that help? –  bill s Aug 6 '13 at 13:17
I have it working with your suggestion, however, it is still a bit. This may of course be the best one can do. –  Sparse Pine Aug 6 '13 at 13:18

Another option is to define a function to do the conversion and use ImageApply to map it over the pixel values:

 f[{0., 0., 0.}] = {0.5, 0.5, 0.5};
 f[x_] := x;
 ImageApply[f, image]]
share|improve this answer

For the record, it's:

ColorReplace[image, RGBColor[{0, 0, 0}] -> RGBColor[{0.5, 0.5, 0.5}]]
share|improve this answer

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