Search code examples
pythontwain

python-twain library in duplex mode get separate image from each side


I am trying to scan using python-twain library in duplex mode and get two images one from each side.

import twain
sm = twain.SourceManager(0)
ss = sm.OpenSource('Plustek MobileOffice D600')

ss.SetCapability( twain.CAP_DUPLEXENABLED, twain.TWTY_BOOL, True )
ss.RequestAcquire(0,0)
rv = ss.XferImageNatively()
if rv:
    (handle, count) = rv
    twain.DIBToBMFile(handle, 'image.bmp') 

The code only get one image but with the library documentation provided at http://twainmodule.sourceforge.net/ , I don't see how to get take independently images from it. I know is possible because I could get in a demo from a close source library CLScan(http://www.commandlinescanning.com).

Any suggestions will be welcome.


Solution

  • I found the sample code TwainBackend.py on GitHub. You can use a loop to save all available images:

    import twain
    
    index = 0;
    
    def next(ss):
        try:
            print ss.GetImageInfo()
            return True
        except:
            return False
    
    def capture(ss):
        global index
        rv = ss.XferImageNatively()
        fileName = str(index) + '_image.bmp';
        index+=1;
        print rv;
        if rv:
            (handle, count) = rv
            twain.DIBToBMFile(handle, fileName) 
    
    sm = twain.SourceManager(0)
    ss = sm.OpenSource('Plustek MobileOffice D600')
    
    try:    
        ss.SetCapability( twain.CAP_DUPLEXENABLED, twain.TWTY_BOOL, True)
    except:
        print "Could not set duplex to True"
    
    print "acquire image"
    ss.RequestAcquire(0,0)
    
    while next(ss):
        capture(ss)
    
    print('Done')