Python Sample: Acquire and save to BMP file

Top  Previous  Next

The following Python 2.5 ( or above) script is all that is required to do the following:

 

Check if TWAIN is installed.

Initialize the DTWAIN Dynamic Link Library.

Select a Source

Acquire a page using the Native transfer mode

Save page to a BMP file called "test.bmp"

Shut down any open TWAIN Source's and DTWAIN itself

 


 

from ctypes import *

import DTWAIN32

mydll = windll.LoadLibrary("dtwain32.dll")

isAvail = mydll.DTWAIN_IsTwainAvailable( )

if isAvail:

   mydll.DTWAIN_SysInitialize( )

   TwainSource = mydll.DTWAIN_SelectSource( )

   if TwainSource:

        mydll.DTWAIN_AcquireFile( TwainSource, "TEST.BMP", DTWAIN32.DTWAIN_BMP, DTWAIN32.DTWAIN_USELONGNAME, DTWAIN32.DTWAIN_PT_DEFAULT, 1, 1, 1, 0 )

   mydll.DTWAIN_SysDestroy( )

 


Note that the cTypes facility of Perl 2.5 and above is used to load the DTWAIN32.DLL and call the DTWAIN functions.  Since the DTWAIN functions all use the stdcall calling convention, the windll.LoadLibrary must be used to correctly load DTWAIN32.DLL (do not use the cdll version of LoadLibrary!).

 

Also, the import DTWAIN32 imports the constants defined by the DTWAIN library.  For the import to succeed, the DTWAIN32.py module contains the constant definitions, and should be located in your Python's path for imports (or in the same directory as your Python scripts).