Pointer to Image

Thomas Heller theller at python.net
Fri Oct 22 14:45:49 EDT 2004


cjankowski at hbr-inc.com (Chris Jankowski) writes:

> The part that is confusing to me, still has not been answered.  
>
> This DLL file has several functions. One is a handler that needs to be
> set to a Python function.  How do I pass it an address to make this
> happen?

It's not that complicated.  First, you have to declare the callback
function's prototype - you find it in the include file for the dll, and
you have to convert it into ctypes notation.  In this sample, the
callback receives an integer as parameter, and returns an integer as
result:

#  typedef int (* OCRProgressHandler)(int percent);
OCRProgressHandler = CFUNCTYPE(c_int, c_int)

Then, you define a Python function which will handle the events:

def py_handler(percent):
    # do something with 'percent', which will be an integer
    return 42

Next, you create the C callback function itself:

callback = OCRProgressHandler(py_handler)

And finally, you can install the callback:

ocrdll.SetProgressHandler(callback) # or something like that

Chris sent me a link where to download the dll (actually a whole OCR
application, the dll is part of it): wocar25.zip
<http://ccambien.free.fr/wocar25.zip>, plus a header file (which seems
not to be included in the application, but may be available elsewhere).

I made a script which passes one of the example tif's to the OCR
functions, and it seems to work quite well.
Since the script is not too large, I'll append it at the end of this
post.  I tried to insert comments to explain what's going in, I hope it
helps to get started.

Thomas

PS: The first paragraph of the OCR output is this - nice, although it
doesn't recognize fi ligatures (is that the correct terminus?):

Artificial Neural Networks
Neural network technology is at the centre of attention in many parts of both the world
scienti<?-5?>c community and the world industrial community. This intensive activity is partly a
response to the need of industry to tind solutions for a huge variety of hard information-
handling problems.

<script>
from ctypes import *

######## some definitions from the include file, converted to ctypes ########

# callback function  prototypes

## typedef int (* OCRProgressHandler)(int percent);
OCRProgressHandler = CFUNCTYPE(c_int, c_int)

## typedef void (* OCROutputHandler)(int infotype,int param);
OCROutputHandler = CFUNCTYPE(None, c_int, c_int)

## typedef void (* OCRErrorHandler)(const char *module,
##                                  const char* fmt, va_list);
OCRErrorHandler = CFUNCTYPE(None, POINTER(c_char),
                            POINTER(c_char), POINTER(c_char))

## typedef void (* OCRWarningHandler)(const char *module,
##                                    int level,const char* fmt, va_list);
OCRWarningHandler = CFUNCTYPE(None, POINTER(c_char),
                              c_int, POINTER(c_char), POINTER(c_char))

# Allowed languages
NONE = 0
ENGLISH = 1
FRENCH = 2
DUTCH = 3

# OCR Events
OT_TEXT = 0
OT_PROP = 1
OT_ITAL = 2
OT_BOLD = 3
OT_UNDS = 4
OT_SIZE = 5
OT_HILT = 6
OT_ENDL = 7
OT_ENDZ = 8
OT_BITM = 9

# Output modes
OM_TEXT = 0
OM_RICHTEXT = 1
OM_WINDOW = 2

######## load the ocr dll.
# ocrdll.dll needs another private dll - so we add the directory to $PATH

import os, sys
os.environ["PATH"] = "c:\wocar"
ocrdll = CDLL(r"c:\wocar\ocrdll.dll")

# load an image

##img =  ocrdll.LoadImg(r"c:\chris_ocr.tif")
img =  ocrdll.LoadImg(r"c:\wocar\samples\english.tif")

# define a Python event handler
def callback(event, param):
    if event == OT_TEXT:
        try:
            sys.stdout.write(chr(param))
        except ValueError:
            sys.stdout.write("<?%d?>" % param)
    elif event in (OT_ENDL, OT_ENDZ):
        sys.stdout.write("\n")

# create a C callback function from it, and install it as Output Handler
callback = OCROutputHandler(callback)
ocrdll.OCRSetOutputHandler(callback)

# scan the image
ocrdll.OCR(img)

# cleanup totally omitted...
<script/>



More information about the Python-list mailing list