Let's say I have an image that is basically two colors, red and white. However there are also parts of the image that are a little bit off white. I want to change those parts to be white, so that the image only has two colors. What program can do that from the command line?

up vote 4 down vote accepted

convert -posterize 2

hmm, that didn't work as expected

convert -colors 2 +dither gradient.png bicolor.png

meh, there is an intensity mapping that forces white into grey thus

# create a 2 color image from scratch to supply colors for mapping
$ convert -size 10x10 xc:white -fill red -draw 'rectangle 0 0 5 5' \
    -colors 2 +dither bicolor.gif
# map 'em, Danno!
$ convert -colors 2 -normalize +dither -remap bicolor.gif in.png out.png

worked. Note: a prior version of this answer used mogrify instead of convert, I changed it because mogrify overwrites the original in-place while convert doesn't but they use the same algorithms and arguments.


bicolor.gif bicolor.gif
in.png in.png
out.png out.png

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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