Negating color range from command line

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
DahdeKing

Negating color range from command line

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Negating color range from command line

Post by fmw42 »

convert imagename.suffix -threshold 100% outimage.suffix

The resulting image will be completely black.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Negating color range from command line

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
DahdeKing

Re: Negating color range from command line

Post 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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Negating color range from command line

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply