Swapping Black and White - nothing more

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
briane678

Swapping Black and White - nothing more

Post by briane678 »

I have a gif image that I want to convert into jpg, and while doing so, swap the colors black and white. I know that '-negate' would do that, but I want to leave all the colors unchanged, and '-negate' does EVERYTHING. I just want to switch black and white. Blue stays blue, red stays red...

Thanks,
briane678
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Swapping Black and White - nothing more

Post by fmw42 »

if you have no transparency in your image, then

convert image.gif -channel rgba -transparent black -fill black -opaque white -fill white -opaque none -alpha off result.gif
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Swapping Black and White - nothing more

Post by anthony »

Two swap two of anything you need somewhere to save one of the colors for later use.

For example if the color '#000000000001' does not exist in the image (unlikely as most images read in from a file only use 8 bit color values and not a 16 bit color value as this color is) then you can swap colors using..

Code: Select all

convert input_image
   -fill '#000000000001' -opaque white \
   -fill white                 -opaque black \
   -fill black                 -opaque '#000000000001' \
   output_image
As Fred (fwm42) points out if the image does not use alpha channel you can use a 'none' or fully transparent color as the save. This also has the advantage of making the alpha channel a mask (negated mask actually) of the saved color.

The -channel RGBA Fred also provided is needed if you are using -opaque with transparent color values (color 'none'). the -transparent operator automatically enables the alpha channel ( -alpha set ) and internally uses -channel RGBA.

The equivelent command for Fred in the above format is...

Code: Select all

convert input_image -channel rgba -alpha set \
            -fill none -opaque white \
            -fill white -opaque black \
            -fill white -opaque none \
            -alpha off output_image
If transparency is being used in your image you will need to find some other color whcih is not being used. If the transparency is only boolean (fully on or off) which is the case with GIF then you can use a half-transparent color as the save color. As such this (as well as the first) should work for any GIF, or JPEG image, as well as most PNG and TIFF images.

Code: Select all

convert input_image
   -fill '#0008' -opaque white \
   -fill white   -opaque black \
   -fill black   -opaque '#0008' \
   output_image
ALL these 'swap two color via unused color' methods can be used with mogrify to do a whole directory of images.

Another method is to create a mask image of one of the color, to store the pixels that was that color. You can use +transparent for this. examples of which are in IM examples Replacing Colors
http://www.imagemagick.org/Usage/color/#replace
As well as further down in 'Fuzz Factor" explanation
http://www.imagemagick.org/Usage/color/#fuzz

Another way of looking at fred's solution is to think of the alpha channel as the color save mask,
so in a sense his solution uses BOTH color values and color masking to achieve the swapping of colors.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
boarderben
Posts: 3
Joined: 2014-05-18T23:25:49-07:00
Authentication code: 6789

Re: Swapping Black and White - nothing more

Post by boarderben »

I used the methods described by fmw42 and anthony on GIF images with colored objects and pure white colored texts on pure black background. After the conversion, the colored objects are preserved fine. However, the black colored texts converted from the white texts (of various font sizes) all show up as "blurred" with vertical white lines in the middle or along the edge.

I tried the conversion on GIF images created by Microsoft Paint and by screenshot capture software. All have the same problem with the text.

Can this problem be corrected? Or should I try bitmap images instead?

Thanks...Ben
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Swapping Black and White - nothing more

Post by snibgo »

You might put an example input and output somewhere like dropbox.com and paste the URLs here.

The problem is probably that your text is anti-aliased, which means that blocking is avoided by a gradual transition, like blurring. If you enlarge your inputs you can probably see this. Changing colours that are exactly white won't change the anti-aliased pixels.
snibgo's IM pages: im.snibgo.com
boarderben
Posts: 3
Joined: 2014-05-18T23:25:49-07:00
Authentication code: 6789

Re: Swapping Black and White - nothing more

Post by boarderben »

I'd like to thank snibgo for the suggestion. I have uploaded original GIF and PNG images and their converted results to the dropbox website.

In the example, I used different fonts in black and white, and added some colored text.

The links for the original GIF/PNG images and their converted results are:

https://www.dropbox.com/s/m040kysa7qvt3 ... nalGIF.gif
https://www.dropbox.com/s/l9eykkduvpa0dzi/resultGIF.gif

https://www.dropbox.com/s/fc5b4sq11nmcy ... nalPNG.png
https://www.dropbox.com/s/sdtrv7sx8grpwz3/resultPNG.png

The results show that the "blurring" effect seemingly only happens to text of certain fonts, like Arial and Time New Roman. Both black-white text and colored text in those fonts encountered the problem. And the "blurring" effect only show up in the vertical direction.

The results also show that the "blurring" effect does not happen to Sans Serif font type. Does that mean it may have something to do with font type?

Thanks in advance,

-Ben
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Swapping Black and White - nothing more

Post by fmw42 »

The unix command is wrong. Probably a typo. It should be


convert originalPNG.png -channel rgba -alpha set \
-fill none -opaque white \
-fill white -opaque black \
-fill black -opaque none \
-alpha off originalPNG_proc.png

But the issue is the antialising of the text. That leaves values between white and black at the edges. You can mitigate this by adding -fuzz, but that may "eat away" at the edges some in the colored fonts. Try finding a fuzz value that suits or works best.

Code: Select all

convert originalPNG.png -channel rgba -alpha set -fuzz 50% \
-fill none -opaque white  \
-fill white -opaque black  \
-fill black -opaque none \
-alpha off originalPNG_proc.png
It is very odd that this loses the green, but I guess it has to do with such a large fuzz value.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Swapping Black and White - nothing more

Post by snibgo »

As I suspected, the issue is antialiasing.

It could be the font type, or the way the software has chosen to render it, or a user choice. For example, IM will anti-alias by default, but the user can disable it with "+antialias".

This antialiasing is weird. Black characters on a white background should give grey antialiasing, but yours have colours. As you wanted in your OP, these have not changed.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Swapping Black and White - nothing more

Post by fmw42 »

Here is another way to do it, but it leaves some purple color in the black/white text antialiased areas.

Reverse the intensity where the saturation is low.

Code: Select all

convert originalPNG.png -colorspace HSI -separate +channel \
\( -clone 2 -negate \) \
\( -clone 1 -threshold 1% -negate \) \
\( -clone 2 -clone 3 -clone 4 -compose over -composite \) \
-delete 3,4 +swap +delete -set colorspace HSI -combine -colorspace sRGB  originalPNG_proc2.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Swapping Black and White - nothing more

Post by anthony »

a 50% fuzz will include green as it is not a pure green but a half bright green, placing it closed to a black color than either pure red or blue.

The mistake is not in using "none" but in not using using '#0008' in the first line
by using '#0008' we are being sure not to match a 'none' color.

However fuzz factors and transparency is tricky and not recommended, especially with such high fuzz levels.
See Fuzz Factor and semi-transparent colors
http://www.imagemagick.org/Usage/color_ ... fuzz_alpha

----

The better solution which fred (fwm42) has attempted to do is not just swap black and white, but reverse any non-color (gray) colors in the image.

However perhaps what you really want is just making light dark and dark light. in which case just revering the intensity channel in HSI may be what you want. It will make light colors darker, and dark colors lighter, but this may work better overall.

Code: Select all

convert originalPNG.png -colorspace HSI -channel B -level 100,0%  +channel -colorspace sRGB reversed_intensity.png
However I am seeing these vertical lines you talk about. I think the image may be using some sub-pixel rendering for LCD displays, that is a finer anti-aliasing based on using LCD RGB displays. It is especially prevelent in the font rendering algorithm used in microsoft and Mac font renderers, as it makes fonts look 'sharper', especially on Mac's where they have control of the display hardware that the user is using. Its a form of 'cheat' that makes further image work very difficult.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
boarderben
Posts: 3
Joined: 2014-05-18T23:25:49-07:00
Authentication code: 6789

Re: Swapping Black and White - nothing more

Post by boarderben »

Many thanks to fmw42, snibgo and anthony for taking time and providing solutions.

For my engineering related apps, I find the intensity reversal method suggested by anthony and fmw42 works the best. I would also use the fuzz adjustment solution from fmw42 for certain situations, as it preserves colors nicely and renders crispy text. I understand that use of high fuzz values can wash out some faint colored thin/dotted lines.

Now I'm happy :D .

Cheers,

-Ben
Post Reply