Twain模块提出异常:twain.excTWCC_SEQERROR

时间:2016-01-22 13:33:50

标签: python twain

我使用python中的twain module开发了一个软件。该软件基于PyQt4。我已连接扫描仪并使用以下代码扫描大量文件:

import twain

sm = twain.SourceManager(0)
ss = sm.OpenSource()
ss.RequestAcquire(0,0)

while True:
    rv = ss.XferImageNatively()
    if rv is None:
        break
    else:
        (handle, count) = rv
        twain.DIBToBMFile(handle, '{}.bmp'.format(uuid.uuid4()))  

扫描操作完成后,图像保存为.bmp文件。 Everthing没问题,但抛出异常,程序无法执行其他代码行。异常消息是:

C:\Python34\python.exe C:/Users/PB/PycharmProjects/34Deneme/dene.py
Traceback (most recent call last):
  File "C:/Users/PB/PycharmProjects/34Deneme/dene.py", line 13, in <module>
    rv = ss.XferImageNatively()
  File "C:\Python34\lib\site-packages\twain.py", line 1841, in XferImageNatively
    rv, handle = self._get_native_image()
  File "C:\Python34\lib\site-packages\twain.py", line 1787, in _get_native_image
    (TWRC_XFERDONE, TWRC_CANCEL))
  File "C:\Python34\lib\site-packages\twain.py", line 1453, in _call
    return self._sm._call(self._id, dg, dat, msg, buf, expected_returns)
  File "C:\Python34\lib\site-packages\twain.py", line 2094, in _call
    raise exc
twain.excTWCC_SEQERROR
Exception ignored in: <bound method SourceManager.__del__ of <twain.SourceManager object at 0x004EEA10>>
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\twain.py", line 2059, in __del__
  File "C:\Python34\lib\site-packages\twain.py", line 2066, in close
  File "C:\Python34\lib\_weakrefset.py", line 101, in pop
KeyError: 'pop from empty WeakSet'

Process finished with exit code 1

那么问题是什么?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

    import twain

sm = twain.SourceManager(0)
ss = sm.OpenSource()
ss.RequestAcquire(0,0)
try:
  while True:
    rv = ss.XferImageNatively()
    if rv is None:
        break
    else:
        (handle, count) = rv
        twain.DIBToBMFile(handle, '{}.bmp'.format(uuid.uuid4()))  
except:
  print "Error!"