# Quickly converting pixels of one value to another [closed]

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


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).

-
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

## 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.

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.

-
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:

Module[{f},
f[{0., 0., 0.}] = {0.5, 0.5, 0.5};
f[x_] := x;
ImageApply[f, image]]

-
ColorReplace[image, RGBColor[{0, 0, 0}] -> RGBColor[{0.5, 0.5, 0.5}]]