473,408 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Pointer to Image

Hi all,
I am still new to using Python, so bare with me. I am trying to call
a DLL from my program to use some of it's functions. The only
parameter is supposed to be a pointer to an image. I have tried
several variations on passing this parameter to the DLL and I am not
having much luck. This is going to call other functions later for
OCR, but I want to get a simple one working first.

Here is my code: (Very basic)

from ctypes import *
import Image

myOcr = windll.LoadLibrary('ocrdll.dll') # my DLL

mySize = myOcr.GetImgBitmapSize ## load the function

im = Image.open('C:\\chris.tif') ## load the Image
print id(im) ## for testing print address of im

##myFile = c_char_p(id(im)) # I have tried several variations of this

x = mySize(myFile) ## Actual call to the DLL Function
## documentation only one parameter passed
pointer to Image
## Must be DIB Image(Device Independent Bitmap)
## (could this be the problem?)
Thanks for the help.

Chris J.
Jul 18 '05 #1
9 2778
Chris Jankowski wrote:
Hi all,
I am still new to using Python, so bare with me. I am trying to call
a DLL from my program to use some of it's functions. The only
parameter is supposed to be a pointer to an image. I have tried
several variations on passing this parameter to the DLL and I am not
having much luck. This is going to call other functions later for
OCR, but I want to get a simple one working first.

Here is my code: (Very basic)

from ctypes import *
import Image

myOcr = windll.LoadLibrary('ocrdll.dll') # my DLL

mySize = myOcr.GetImgBitmapSize ## load the function

im = Image.open('C:\\chris.tif') ## load the Image
print id(im) ## for testing print address of im

##myFile = c_char_p(id(im)) # I have tried several variations of this

x = mySize(myFile) ## Actual call to the DLL Function
## documentation only one parameter passed
pointer to Image
## Must be DIB Image(Device Independent Bitmap)
## (could this be the problem?)
Thanks for the help.

Chris J.


Can you supply any docs on the ocrdll.dll API (e.g example C
code or function/parameter documentation)? I've interfaced
with ExperVision's OCR RTK Toolkit via calls to their .DLL
but it looks like you are using something different.

-Larry
Jul 18 '05 #2
cj********@hbr-inc.com (Chris Jankowski) writes:
Hi all,
I am still new to using Python, so bare with me. I am trying to call
a DLL from my program to use some of it's functions. The only
parameter is supposed to be a pointer to an image. I have tried
several variations on passing this parameter to the DLL and I am not
having much luck. This is going to call other functions later for
OCR, but I want to get a simple one working first.

Here is my code: (Very basic)

from ctypes import *
import Image

myOcr = windll.LoadLibrary('ocrdll.dll') # my DLL

mySize = myOcr.GetImgBitmapSize ## load the function

im = Image.open('C:\\chris.tif') ## load the Image
print id(im) ## for testing print address of im

##myFile = c_char_p(id(im)) # I have tried several variations of this

x = mySize(myFile) ## Actual call to the DLL Function
## documentation only one parameter passed
pointer to Image
## Must be DIB Image(Device Independent Bitmap)
## (could this be the problem?)
Thanks for the help.


I assume the Image function you import in the code above is from PIL?
In this case, it cannot work. You cannot take the address (id) of an
arbitrary Python object, and assume that it points to C compatible data.
Maybe you should use a function from the dll to load the image data, or
you can read the file's bytes into python string, and pass that.

Thomas
Jul 18 '05 #3
Larry Bates <lb****@syscononline.com> wrote in message news:<0b********************@comcast.com>...
Chris Jankowski wrote:
Hi all,
I am still new to using Python, so bare with me. I am trying to call
a DLL from my program to use some of it's functions. The only
parameter is supposed to be a pointer to an image. I have tried
several variations on passing this parameter to the DLL and I am not
having much luck. This is going to call other functions later for
OCR, but I want to get a simple one working first.

Here is my code: (Very basic)

from ctypes import *
import Image

myOcr = windll.LoadLibrary('ocrdll.dll') # my DLL

mySize = myOcr.GetImgBitmapSize ## load the function

im = Image.open('C:\\chris.tif') ## load the Image
print id(im) ## for testing print address of im

##myFile = c_char_p(id(im)) # I have tried several variations of this

x = mySize(myFile) ## Actual call to the DLL Function
## documentation only one parameter passed
pointer to Image
## Must be DIB Image(Device Independent Bitmap)
## (could this be the problem?)
Thanks for the help.

Chris J.


Can you supply any docs on the ocrdll.dll API (e.g example C
code or function/parameter documentation)? I've interfaced
with ExperVision's OCR RTK Toolkit via calls to their .DLL
but it looks like you are using something different.

-Larry


Here is the website with the documentation.

http://www.simpleocr.com/Help.asp

There are two .DLL files needed
ocrdll.dll
Dllwain.dll

please let me know if there is anything else.
Jul 18 '05 #4
Larry Bates <lb****@syscononline.com> wrote in message news:<0b********************@comcast.com>...
Chris Jankowski wrote:
Hi all,
I am still new to using Python, so bare with me. I am trying to call
a DLL from my program to use some of it's functions. The only
parameter is supposed to be a pointer to an image. I have tried
several variations on passing this parameter to the DLL and I am not
having much luck. This is going to call other functions later for
OCR, but I want to get a simple one working first.

Here is my code: (Very basic)

from ctypes import *
import Image

myOcr = windll.LoadLibrary('ocrdll.dll') # my DLL

mySize = myOcr.GetImgBitmapSize ## load the function

im = Image.open('C:\\chris.tif') ## load the Image
print id(im) ## for testing print address of im

##myFile = c_char_p(id(im)) # I have tried several variations of this

x = mySize(myFile) ## Actual call to the DLL Function
## documentation only one parameter passed
pointer to Image
## Must be DIB Image(Device Independent Bitmap)
## (could this be the problem?)
Thanks for the help.

Chris J.


Can you supply any docs on the ocrdll.dll API (e.g example C
code or function/parameter documentation)? I've interfaced
with ExperVision's OCR RTK Toolkit via calls to their .DLL
but it looks like you are using something different.

-Larry


I think I may have it. cdll instead of windll
Jul 18 '05 #5
Thomas Heller <th*****@python.net> wrote in message news:<d5**********@python.net>...
cj********@hbr-inc.com (Chris Jankowski) writes:
Hi all,
I am still new to using Python, so bare with me. I am trying to call
a DLL from my program to use some of it's functions. The only
parameter is supposed to be a pointer to an image. I have tried
several variations on passing this parameter to the DLL and I am not
having much luck. This is going to call other functions later for
OCR, but I want to get a simple one working first.

Here is my code: (Very basic)

from ctypes import *
import Image

myOcr = windll.LoadLibrary('ocrdll.dll') # my DLL

mySize = myOcr.GetImgBitmapSize ## load the function

im = Image.open('C:\\chris.tif') ## load the Image
print id(im) ## for testing print address of im

##myFile = c_char_p(id(im)) # I have tried several variations of this

x = mySize(myFile) ## Actual call to the DLL Function
## documentation only one parameter passed
pointer to Image
## Must be DIB Image(Device Independent Bitmap)
## (could this be the problem?)
Thanks for the help.


I assume the Image function you import in the code above is from PIL?
In this case, it cannot work. You cannot take the address (id) of an
arbitrary Python object, and assume that it points to C compatible data.
Maybe you should use a function from the dll to load the image data, or
you can read the file's bytes into python string, and pass that.

Thomas


Here is what I have now.

from ctypes import *
def myOutputHandler(infotype, param):
outputFile.write("outhandler")
if infotype == "OT_TEXT":
print param
## return param
## elif infotype == "OT_ENDL":
## return "\n"
## elif infotype == "OT_ENDZ":
## return "\n\n"
return
#************************************************* **************
#* main process *
#* myList - requests *
#* inList - data file *
#************************************************* **************
if __name__ == '__main__':
outputFile=open('c:\\newOCR.txt', 'w')
myOcr = cdll.LoadLibrary('C:\\SimpleOCR\\ocrdll.dll')
loadImg = myOcr.LoadMultipleImg
x = loadImg("c:\\chris.tif")
print x
setLanguage = myOcr.SetLanguage
setLanguage('ENGLISH',".")

setOutputMode = myOcr.SetOutputMode
setOutputMode("OM_TEXT")

setOutputHandler = myOcr.OCRSetOutputHandler
z = id(myOutputHandler)
errorx = setOutputHandler(z)

getImage = myOcr.GetImage
img = getImage(x,0)

getNBImages = myOcr.GetNbImages
numImg = getNBImages(x)

processOCR = myOcr.OCR
myTest = processOCR(img) ## it will run without this line.

freeMultipleImage = myOcr.FreeMultipleImg
freeMultipleImage(x)

outputFile.close()

This will not run it abends when I try to call the .OCR function. I
am thinking that it cannot call the procedure to write the file. Any
ideas?
Jul 18 '05 #6
cj********@hbr-inc.com (Chris Jankowski) writes:
Here is what I have now.

from ctypes import *
def myOutputHandler(infotype, param):
outputFile.write("outhandler")
if infotype == "OT_TEXT":
print param
## return param
## elif infotype == "OT_ENDL":
## return "\n"
## elif infotype == "OT_ENDZ":
## return "\n\n"
return
#************************************************* **************
#* main process *
#* myList - requests *
#* inList - data file *
#************************************************* **************
if __name__ == '__main__':
outputFile=open('c:\\newOCR.txt', 'w')
myOcr = cdll.LoadLibrary('C:\\SimpleOCR\\ocrdll.dll')
loadImg = myOcr.LoadMultipleImg
x = loadImg("c:\\chris.tif")
print x
setLanguage = myOcr.SetLanguage
setLanguage('ENGLISH',".")

setOutputMode = myOcr.SetOutputMode
setOutputMode("OM_TEXT")

setOutputHandler = myOcr.OCRSetOutputHandler
z = id(myOutputHandler)
errorx = setOutputHandler(z)

getImage = myOcr.GetImage
img = getImage(x,0)

getNBImages = myOcr.GetNbImages
numImg = getNBImages(x)

processOCR = myOcr.OCR
myTest = processOCR(img) ## it will run without this line.

freeMultipleImage = myOcr.FreeMultipleImg
freeMultipleImage(x)

outputFile.close()

This will not run it abends when I try to call the .OCR function. I
am thinking that it cannot call the procedure to write the file. Any
ideas?


At least one problem that I see is that you are passing
id(myOutputHandler) to setOutputHandler call. Assuming that
setOutputHandler expects a C callback function, this will not work.

Ok - send me (in private email) a pointer to where I can download the
ocrdll.dll including C header files, plus your 'chris.tif' image, and I
will take a look at it.

Thomas
Jul 18 '05 #7
Chris Jankowski wrote:
# [...]
setLanguage = myOcr.SetLanguage
setLanguage('ENGLISH',".")

setOutputMode = myOcr.SetOutputMode
setOutputMode("OM_TEXT")

setOutputHandler = myOcr.OCRSetOutputHandler
z = id(myOutputHandler)
errorx = setOutputHandler(z)
# [...]


As a minor stylistic note, I find your constant use of temporary local
names of module functions to be distracting and even slightly
confusing. When you actually *call* a function, it's not immediately
apparent whether that's a helper function that you've written yourself
or whether it's a function in your OCR module -- I have to backtrack to
see where the function came from, and I'm never quite sure whether I
will find it somewhere in the current code block (because you've bound
the imported function to a local name) or whether I should look
elsewhere in the current module (because it's a function you've defined
yourself).

I'd find the above code snippet *much* easier to read without the
temporaries:

myOcr.SetLanguage('ENGLISH', ".")
myOcr.SetOutputMode("OM_TEXT")
errorx = myOcr.OCRSetOutputHandler( id(myOutputHandler) )

To my eyes, this makes it much more clear where everything is found, and
because it's so much shorter it's much easier to take in at a glance.

(I also share Thomas Heller's suspicion that passing the id() of
myOutputHandler() is *not* what you want to do here. The ID is merely
an integer that Python uses internally to track an object; it is
typically not useful for code that wants to *use* an object to know what
its ID is. You don't need to know your co-workers' social security
numbers in order to work with them effectively, right? Similarly, a
Python object's ID is only needed in very special circumstances where
the exact identity of an object is in question, and it's *not* needed
for normal usage of an object such as being able to call a function object.)

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #8
Jeff Shannon <je**@ccvcorp.com> wrote in message news:<10*************@corp.supernews.com>...
Chris Jankowski wrote:
# [...]
setLanguage = myOcr.SetLanguage
setLanguage('ENGLISH',".")

setOutputMode = myOcr.SetOutputMode
setOutputMode("OM_TEXT")

setOutputHandler = myOcr.OCRSetOutputHandler
z = id(myOutputHandler)
errorx = setOutputHandler(z)
# [...]


As a minor stylistic note, I find your constant use of temporary local
names of module functions to be distracting and even slightly
confusing. When you actually *call* a function, it's not immediately
apparent whether that's a helper function that you've written yourself
or whether it's a function in your OCR module -- I have to backtrack to
see where the function came from, and I'm never quite sure whether I
will find it somewhere in the current code block (because you've bound
the imported function to a local name) or whether I should look
elsewhere in the current module (because it's a function you've defined
yourself).

I'd find the above code snippet *much* easier to read without the
temporaries:

myOcr.SetLanguage('ENGLISH', ".")
myOcr.SetOutputMode("OM_TEXT")
errorx = myOcr.OCRSetOutputHandler( id(myOutputHandler) )

To my eyes, this makes it much more clear where everything is found, and
because it's so much shorter it's much easier to take in at a glance.

(I also share Thomas Heller's suspicion that passing the id() of
myOutputHandler() is *not* what you want to do here. The ID is merely
an integer that Python uses internally to track an object; it is
typically not useful for code that wants to *use* an object to know what
its ID is. You don't need to know your co-workers' social security
numbers in order to work with them effectively, right? Similarly, a
Python object's ID is only needed in very special circumstances where
the exact identity of an object is in question, and it's *not* needed
for normal usage of an object such as being able to call a function object.)

Jeff Shannon
Technician/Programmer
Credit International


Hey Jeff,
Thanks for the pointers. I am still new to Python and sometimes do
things the hard way, because I don't know any better. (yet) I just
copied this from the cytpes documentation and plugged in my variable
names.

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?

def myOutputHandler(infotype, param):
print("outhandler")
if infotype == "OT_TEXT":
print param
##[...]

myOcr.OCRSetOutputHandler(address of myOutputHandler?) ###
??????????????

Thanks,

Chris J.
Jul 18 '05 #9
cj********@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/>
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: koperenkogel | last post by:
Dear cpp-ians, I am writing code for a image segmentation program. For this purpose I want to make a list (or array) of all pixels and take a random pixel out of the list. Once the pixel is...
5
by: wallacej | last post by:
Does anybody know why unsigned char myImage; works but unsigned char myImage; does not? I think its because the second line is a value too big for unsigned char, is this ...
19
by: Sergey Koveshnikov | last post by:
Hello, If my function return a pointer on a memory area, where do I free it? e.x.: char *foo() { char *p; p = malloc(10); strcpy(p, "something"); return(p); }
5
by: Mark Feller | last post by:
I have an embedded application where malloc( ) is not available. So I declare images as globals, but want to be able to pass image information including a pointer in a structure, as in the...
6
by: Itay_k | last post by:
Hello, I want a member in my class that will save pointer to pointer to System::Drawing::Image class. When I write on my class code: System::Drawing::Image **bmp; I get this error message:...
8
by: TJ | last post by:
I need to be able to pass a pointer to a (managed code) Stream object to a COM method so it can serialize its data into the stream. I have a C# application that makes uses of Randolf Duke's...
4
by: Manuel | last post by:
I think I can't find this on google/books because is soooooooo basic... This function assign a listner pointer to an image widget: ------------------------------------------------------ void...
4
by: aneuryzma | last post by:
Hi, this is my code: IplImage *image = 0; .... if( !image ) { image = cvCreateImage( cvGetSize(frame), 8, 3 );
7
by: agitlin | last post by:
Hello All, I'm working with a SDK for a piece of hardware attached to a Windows CE device. The sample code provided is all written in eVC++ and I'm trying to port it over to a VB.NET...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.