Inverting Image Colors Before OCR

Anyone know of a way to invert the colors of a screenshot image before sending it to OCR? The Tesseract OCR is only trained on dark text on a light background and performs pretty horribly when it is light text on a dark background.

2 Likes

@Ryan_P, Did you find the solution?

About controlling Preview via Applescript: Help: Change the Color of the Selected Object in Preview

I also have this same question and problem. It's a serious limitation of the OCR feature for automation purposes.

I am wondering if anyone got any further with this? I.e. Automating the Inversion of Captured Images prior to applying OCR?

Currently applying OCR via Keyboard Maestro to this image:

image

Returns: "21/"or "paw" "Pv" i.e. different and wrong every time.

Applying OCR to the same image, inverted:

image

returns the answer "217" consistantly.

I am wondering if there is any Script that could be called by Keyboard Maestro to invert a captured image before applying the OCR?

It's exactly the same question asked at the head of this thread but so far no answers other than manually inverting the image each time.

(I've tried searching for AppleScripts that might do this and could be incorporated into Keyboard Maestro but again no luck so far.)

Hi Zabobon

If you have Imagemagick installed it can be done with this macro.

OCR inverted screenshot Macro (v9.2)

OCR inverted screenshot.kmmacros (2.3 KB)

1 Like

Thanks @JimmyHartington - I looked for Imagemagick but the installation of it seemed a bit scary for me as I wouldn't know what I was doing following the installation steps...

But maybe your approach can work with other Apps. Ideally making use of Preview. Keyboard Maestro has some image manipulation built-in but not inverting.

For installation of commandline tools I use the package manager Homebrew.
So first install homebrew from this link: http://brew.sh
Then run the command brew install imagemagick in terminal.

1 Like

I found on my system Automator has the ability to invert colors of an image with a Quartz filter.

So in the macro below I use the attached Automator workflow to invert the colors of the image which is screenshot.

Automator invert colors.workflow.zip (33.3 KB)

OCR inverted screenshot Automator Macro (v9.2)

OCR inverted screenshot Automator.kmmacros (2.5 KB)

3 Likes

Wow! Perfect! Thank you so much @JimmyHartington

This works without any new software having to be installed!

After downloading your Macro and Automator Workflow I unzipped the Automator invert colors.workflow.zip and put the .workflow file with my other Automator Workflows in a folder in Dropbox. Then I pointed Keyboard Maestro to that Automator file in the second of your Macro's Actions.

Thanks again - I'd say this Solves the question that @Ryan_P started this thread with. :grinning:

1 Like

This has been working reasonably well for me, with appropriate customizations for where I want the temp file and the automated workflow to be saved.

However, it glitches aesthetically for me. Reading dark on light text can be nearly a 1-action macro, reading the image from the clipboard and writing the text to the clipboard.

Does anyone know of any way to apply that quartz filter to the clipboard instead of a temp file?

Hi @August - since I posted the post above yours, I've stopped using this method and now use Keyboard Maestro in combination with Monterey's amazing OCR capabilities. Of course you have to have Monterey to have it work.

I've found it can not only deal with light letters on dark but can extract text from pretty much any image - like a Google Map. It even deals with strange gothic fonts and handwriting.

This:

image

Gets this:

INTRODUCING
Cambridge
DECORATIVE, BOLD AND STRONG
GOTHIC FONT

And this:

image

Still gives this:

INTRODUCING
Cambridge
DECORATIVE, BOLD AND STRONG
GOTHIC FONT

Here is a link to how I implement it. There are other methods posted on the Forum.

You can even make it a subroutine and call it from any other Macros that needs OCR. I have it as a subroutine of a Macro I use when making notes on a video edit and the info I need to capture is in captions over the video. In that case it is set to grab from a particular area of the movie frame where the caption always is. Before using the Monterey Shortcut/Keyboard Maestro combination it was hit and miss.

But, as I said already, you have to have Monterey for it to work :grinning:

2 Likes

Thanks for this, works great as a Quick action