Page 1 of 1
How to change the background and foreground colors
Posted: 2017-06-02T23:00:05-07:00
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?
Re: How to change the background and foreground colors
Posted: 2017-06-03T10:43:08-07:00
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.
Re: How to change the background and foreground colors
Posted: 2017-06-03T10:48:00-07:00
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
Re: How to change the background and foreground colors
Posted: 2017-06-03T11:30:49-07:00
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
Re: How to change the background and foreground colors
Posted: 2017-06-03T11:44:46-07:00
by snibgo
Another approach: make it gray, and auto-level:
Code: Select all
convert 958.png -colorspace Gray -auto-level 958_gr.png
Re: How to change the background and foreground colors
Posted: 2017-06-03T12:01:46-07:00
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
Re: How to change the background and foreground colors
Posted: 2017-06-03T12:20:39-07:00
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
Re: How to change the background and foreground colors
Posted: 2017-06-03T12:41:12-07:00
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.
Re: How to change the background and foreground colors
Posted: 2017-06-03T12:54:26-07:00
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.
Re: How to change the background and foreground colors
Posted: 2017-06-04T07:10:33-07:00
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.
Re: How to change the background and foreground colors
Posted: 2017-06-04T10:26:16-07:00
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.