Page 1 of 1

Negating color range from command line

Posted: 2007-12-28T07:22:25-07:00
by DahdeKing
I have set of images that have white letters on them. Backgrounds are different and colored. (No white in backgrounds)

What I'm trying to do is change all color to black.
This almost does the trick, but some white pixels remain as residue in background.

Code: Select all

NameCapture.bmp -gamma 0.001 -monochrome NameCapture_NEW.bmp
This reverses the process changing all white letters to black and omitting the background, but some lightly colored residue remains.

Code: Select all

NameCapture.bmp -gamma 0.001 -level 100%, 0%, 1.0 NameCapture_NEW.bmp


I need absolutely no residue remaining. Any advice would be greatly appreciated.

Re: Negating color range from command line

Posted: 2007-12-28T17:14:15-07:00
by fmw42
convert imagename.suffix -threshold 100% outimage.suffix

The resulting image will be completely black.

Re: Negating color range from command line

Posted: 2008-01-02T18:34:03-07:00
by anthony
I would check that a a color like #FFFFFFFFFFFe does go to black before trusting that command for colors. (I lowercased the 'e' to make it more obvious)

Code: Select all

  convert xc:'#FFFFFFFFFFFe' -threshold 100% txt:
But then so does white!

Code: Select all

  convert xc:'#FFFFFFFFFFFF' -threshold 100% txt:
The negated method however works find for a gray scale value...

Code: Select all

 convert xc:'#FFFeFFFeFFFe' -negate -threshold 0 -negate txt:
(producing black as wanted)
But the one bit color change produced white which is NOT wanted.

Code: Select all

 convert xc:'#FFFFFFFFFFFe' -negate -threshold 0 -negate txt:
Remember threshold is really a grayscale channel operator with a 'fudge' for handling color images. To get get a perfect, not-white, method you can do one of two things..

1/ threshold each channel image separately and then recombine appropraiteally so result is white only if all three channels are white.

Code: Select all

convert xc:'#FFFFFFFFFFFe' \
   -channel RGB  -separate +channel \
   -negate  -threshold 0 -negate \
   -background white -compose multiply -flatten txt:
Try it also with pure white.
This is tricky, and can fail for some specific versions of IM, where the threshold level was not quite 'normal'. Note that the current documentation for threshold is actually wrong!

2/ A simpler solution for images without transparency, is to use the color replacement functions to 'make a hole' for the exact color selected, then invert the transparency, and underlay a black background.

Code: Select all

convert image.png -matte -transparent white \
    -channel A -negate +channel \
    -background black -flatten \
    result.png
this will work for any EXACT color, replacing anything not that color with any other color you like, not just white and black. You can also use a -fuzz factor to broaden the range of colors.

PS: I would like to see +transparent and +opaque changed to mean a 'not this color' so as to remove the need for the transparency channel to invert the result.

Re: Negating color range from command line

Posted: 2008-01-14T02:24:40-07:00
by DahdeKing
Wow, nice ...

I'll be trying a bunch of those in a sec.

but what is the txt: on the end there??

Image

How would I go about negating a backround on an image like this while perfectly preserving the letters?

Re: Negating color range from command line

Posted: 2008-01-14T19:49:23-07:00
by anthony
It was just to report the actual color of the one pixel color image I was using for testing.

You realise your leters will then be white on white?


try solarize. It is a thresholded negate. Anything above a given value is negated.

convert image.png -solarize 50% -negate result.png

the final negate is to make it all white rather than black.