Freebie OCR for screenshots and TIFF/BMP Files

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
gpulawski
Newbie
Posts: 6
Joined: Mon Sep 10, 2007 3:11 am

Freebie OCR for screenshots and TIFF/BMP Files

Post by gpulawski » Mon Oct 08, 2007 12:31 am

I looked into using a couple of the commercial SDK's for some limited OCR of screenshot bitmaps for instance to verify information in a window and to check some image files for certain text content. These were quite expensive and required royalty fees. Then, I recalled that Microsoft Office has built in OCR. It is in Office Tools for version 2003 onward -- in Microsoft Office Document Imaging. START>ALL PROGRAMS>MICROSOFT OFFICE>MICROSOFT OFFICE TOOLS>. The component is called MODI and lives in MDIVWCTL.DLL. It is scriptable!!!

MODI understands TIFF, multi-page TIFF, and BMP. You can search MSDN under MODI for objects and methods.

Here's some example code to open a TIF or BMP file, OCR the first page, and then put the text result into a single string in a message box.

Code: Select all

VBStart
Dim miDoc
Dim miLayout
Dim stringOut

set miDoc=CreateObject("MODI.Document")
' Load an existing TIFF file.
miDoc.Create ("C:\pathname.tif")
' Perform OCR.
'You can change the mousepointer here to an hourglass or something.
miDoc.Images(0).OCR
'Change the mouse back to normal default.
set miLayout = miDoc.Images(0).Layout
stringOut=miLayout.Text
MsgBox(stringOut)
Set miLayout = Nothing
Set miDoc = Nothing
VBEND
MODI also has its own search methods, document viewer, etc. I think Office 2007 and Vista do not install it by default like 2003 does. (I don't own Vista.)

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Oct 08, 2007 7:37 am

Hi,

Thanks for this, this is awesome. MODI (Microsoft Office Document Imaging) is installed with my Office 2007 installation under Vista.

Here's my version of your code that uses OCR to get the text of the active window. Uncomment the comment block to OCR the entire screen:

Code: Select all

VBStart
Function DoOCR(bitmapfile)
  Dim miDoc
  Dim miLayout
  Dim stringOut

  set miDoc=CreateObject("MODI.Document")
  miDoc.Create (bitmapfile)
  ' Perform OCR.
  'You can change the mousepointer here to an hourglass or something.
  miDoc.Images(0).OCR
  'Change the mouse back to normal default.
  set miLayout = miDoc.Images(0).Layout
  stringOut=miLayout.Text
  'MsgBox(stringOut)
  DoOCR = stringOut
  Set miLayout = Nothing
  Set miDoc = Nothing
End Function
VBEND

/*
//This example OCRs entire screen

//Capture Entire Screen to bitmap
GetScreenRes>x,y
ScreenCapture>0,0,x,y,%TEMP_DIR%\~scrn.bmp

//Use OCR to get text from screen
VBEval>DoOCR("%TEMP_DIR%\~scrn.bmp"),TheText

//Display text
MessageModal>TheText

*/

//This example OCRs just the active window
GetActiveWindow>title,X,Y,W,H
ScreenCapture>x,Y,{%X%+%W%},{%Y%+%H%},%TEMP_DIR%\~scrn.bmp

//Use OCR to get text from active window
VBEval>DoOCR("%TEMP_DIR%\~scrn.bmp"),TheText

//Display the text
MessageModal>TheText
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

gpulawski
Newbie
Posts: 6
Joined: Mon Sep 10, 2007 3:11 am

Post by gpulawski » Tue Oct 09, 2007 2:04 am

Yup, I can think of a lot of possibilities. It doesn't compare in accuracy to Omnipage or ABBYY but it's good enough for a lot of interesting Automation/MacroScheduler ideas. (And just about everybody has it installed already.)

GJP

pzelenka
Newbie
Posts: 11
Joined: Mon Nov 25, 2002 9:55 pm
Location: South Dakota

Post by pzelenka » Wed Oct 24, 2007 3:46 pm

This is very cool.

I used this in a script to process Windows exception dialog boxes with text that was otherwise impossible to get. When the exception text is evaluated, the script can then be appropriately branched.

Thank you very much!!

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Oct 24, 2007 4:38 pm

Just in case you weren't aware - with most windows message boxes if you hit CTRL+C the content of the dialog is copied to the clipboard.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

pzelenka
Newbie
Posts: 11
Joined: Mon Nov 25, 2002 9:55 pm
Location: South Dakota

Post by pzelenka » Wed Oct 24, 2007 5:42 pm

I gave the CTRL+C a try, also efforts using GetWindowText and GetControlText approaches. all without success.

When there are exception issues, you will be presented with warning dialog boxes with differing descriptions, usually dependent upon the module, routine or dictionary. The most pressing concern is the proper handling of the Warning dialog boxes.

In the case of the Dialog boxes, the window title is available, but the descriptive content was out of reach, until I used the screen capture and OCR approach.

Thanks

Semper
Junior Coder
Posts: 30
Joined: Mon Feb 25, 2008 3:28 pm

Post by Semper » Wed Feb 27, 2008 11:11 am

Hi,

any idea why i can't run the MODI script wihtout
getting error OCR:bad language at
line miDoc.Images(0).OCR

Thanks for the feedback.

Semper
Junior Coder
Posts: 30
Joined: Mon Feb 25, 2008 3:28 pm

Post by Semper » Sun Mar 02, 2008 11:56 am

I figured it out. The language parameter Id was missing since i'm on a
multilingual machine

miDoc.images(0).OCR(9) did the job. :D

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Post by rullbandspelare » Sat Mar 15, 2008 10:40 pm

I dont have Office 2007 and was looking for OCR. I found this one wich is free to use.
http://www.topocr.com/
It would be interesting if someone could compare the quality of result with the previously posted example for Office 2007.

for example:

Code: Select all

GetActiveWindow>title,X,Y,W,H
ScreenCapture>x,Y,{%X%+%W%},{%Y%+%H%},screen.jpg
run program>topocr.exe screen.jpg screen.txt
ReadFile>screen.txt,screen
MessageModal>%screen%

I was also looking for Barcode reader OCR. And found this:
http://www.metois.com/Eymbarcode/index.htm
This free demo gives a splash screen every now and then but works very well.
Has someone found anything better?

Thanks!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts