How can I apply a mask using drawing tools(or some other custom mask) to color replace part of an image? for example the image enter image description here

Masks returned by DominantColors (via "CoverageImage") aren't quite right, I mainly want to change to color of the glow in the image but it seems like the only way to change a color is to change it globally across the image. Which is an issue because there is white on the egg, lettering and in the center of the glow.

I've looked at ImageApply but it doesn't seem to like using ColorReplace as a pure function, so I'm sort of at a loss.

I have looked at Changing color of an object in an image but that post is 8 years old now and I think there must be an easier method today. Also alot of the code there is over my head so I could use some guidance.


Here is how you can use your mask to apply image operations to only a part of the image:

img = Import["https://i.stack.imgur.com/ovwNY.jpg"];
mask = Import["https://i.stack.imgur.com/e15TY.png"];
ColorNegate[mask] img + ColorReplace[mask img, White -> Red]


  • $\begingroup$ It would be good to see how to improve this solution such that it also works fine when the color to be replaced is Black instead of White. $\endgroup$ Aug 16 at 21:16

I felt weird leaving this as a comment so I'm expanding it (but I'll still give you the answer). Eventually I found the most general way was via the method introduced here https://reference.wolfram.com/language/workflow/ExtractARegionFromAnImage.html

and then using some combination of ImageRecolor or/and ColorReplace.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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