How to change the background and foreground colors

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
Amused23
Posts: 20
Joined: 2015-09-24T22:48:39-07:00
Authentication code: 1151

How to change the background and foreground colors

Post by Amused23 »

Hi I have this image
https://www.dropbox.com/s/k74wc6id98lzsgu/958.png?dl=0

I have changed the background color to white and foreground to black using:

Code: Select all

convert 958.png -colorspace Gray -auto-level -threshold 80% -unsharp 0x5 958WB.png
However my OCR software has trouble digesting it. I have a better chance if the original font is preserved as it is.
All I need is to replace the background (any color) with white and the text with black colors.
How do I go about doing it?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to change the background and foreground colors

Post by fmw42 »

You can do:

Code: Select all

convert 958.png -alpha off -fill white -opaque "rgb(220,173,38)" -fill black +opaque white 958_bw.png
But that will not be very good either, since there is no good way to prevent the blending of colors in your original at the edges of the characters where it is anti-aliased. The above process removes the anti-aliasing and you see stair stepping.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to change the background and foreground colors

Post by fmw42 »

Try this. It keeps the antialiasing, but is grayscale letters on white background.

Code: Select all

convert 958.png -alpha off -fill white -opaque "rgb(220,173,38)" -colorspace gray 958_wg.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to change the background and foreground colors

Post by snibgo »

Another approach is by matching histograms. We make a gradient image, and change 958.png so its histogram roughly matches the histogram of the gradient. We convert the output to gray.

Code: Select all

%IM%convert -size 100x100 gradient: g.png

%PICTBAT%matchHisto 958.png g.png 958_match.png

%IM%convert 958_match.png -colorspace Gray 958_match.png
Image
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to change the background and foreground colors

Post by snibgo »

Another approach: make it gray, and auto-level:

Code: Select all

convert 958.png -colorspace Gray -auto-level 958_gr.png
Image
snibgo's IM pages: im.snibgo.com
Amused23
Posts: 20
Joined: 2015-09-24T22:48:39-07:00
Authentication code: 1151

Re: How to change the background and foreground colors

Post by Amused23 »

Thanks for the reply Fred and snigbo. auto-level was the clincher snigbo. Fred's 2nd approach was close. I have an image of this nature too.
https://www.dropbox.com/s/rv2e6lixnx6hxg6/2694.png?dl=0

can you provide your inputs?

I am on Ubuntu 16.04 and Imagemagick 6.8.9-9 Q16 x86_64
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to change the background and foreground colors

Post by snibgo »

That has text in two different colours. What result do you want? Perhaps you want black text on a white background, or one line of black text and another or gray.

If you want all text to become the same colour, eg black, but with antialiasing it may be better to split the input into images, where the text within an image is a single colour.

If you don't care about antialiasing you can find the background colour, then:

Code: Select all

convert 2694.png -fill Black +opaque rgb(40%,0,0) -fill White +opaque Black out.png
snibgo's IM pages: im.snibgo.com
Amused23
Posts: 20
Joined: 2015-09-24T22:48:39-07:00
Authentication code: 1151

Re: How to change the background and foreground colors

Post by Amused23 »

Actually, I will be processing these images with Java and Tesseract. I cannot stop the workflow to break the image into 2 image. I can resize the image and increase its dpi. Then I need a white background and black text. There can be no varying colors.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to change the background and foreground colors

Post by snibgo »

The splitting would be automatic, of course, as part of the workflow.

But general advice is difficult without knowing the scope of the project.
snibgo's IM pages: im.snibgo.com
Amused23
Posts: 20
Joined: 2015-09-24T22:48:39-07:00
Authentication code: 1151

Re: How to change the background and foreground colors

Post by Amused23 »

Hi folks...Is it possible to make background color of an image transparent with only the foreground text left out. Then make the foreground text black and then flatten the image to get a white background.

https://www.dropbox.com/s/rv2e6lixnx6hxg6/2694.png?dl=0

Can safely assume that the background is a unvarying solid color (but each image has a different one). So we cannot know in advance which color it is.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to change the background and foreground colors

Post by snibgo »

You need to decide what defines the background colour. For example, it might be:

1. The colour that is at top-left (0,0).
2. The most common colour in the image.
3. The average colour around the four edges.
4. The most common colour around the four edges.

Once you have found that, you can make it transparent with "-transparent #ABC", then make all pixels black (either opaque or transparent), then flatten against white.

This is binary, so loses the antialising.
snibgo's IM pages: im.snibgo.com
Post Reply