Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanning and Saving Multiple Files #8

Open
yazilimci1 opened this issue Jan 27, 2016 · 4 comments
Open

Scanning and Saving Multiple Files #8

yazilimci1 opened this issue Jan 27, 2016 · 4 comments

Comments

@yazilimci1
Copy link

I want to scan and save multiple files . If I can give the number of pages in a for loop ,

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

for i in range(3):   //scanned 3 files 
   try:
        rv = ss.XferImageNatively()
        if rv:
            (handle, count) = rv
            twain.DIBToBMFile(handle, '{}.bmp'.format(uuid.uuid4()))
        else:
            break
   except:
        print("Error")

There are no error in this code, working good , scans three file and saves as .bmp file . But user doesn't have to enter number of documents so I must do it dynamically . When I use a infinitive while loop like below,

while True:
    try:
        rv = ss.XferImageNatively()
        if rv:
            (handle, count) = rv
            twain.DIBToBMFile(handle, '{}.bmp'.format(uuid.uuid4()))
        else:
            break
    except:
          print("Error")

This code gives an error , but saves the documents scanned.

Traceback (most recent call last):
  File "C:/Users/PB/PycharmProjects/34Deneme/dene.py", line 18, in <module>
    rv = ss.XferImageNatively()
  File "C:\Python34\lib\site-packages\twain.py", line 1840, in XferImageNatively
    rv, handle = self._get_native_image()
  File "C:\Python34\lib\site-packages\twain.py", line 1786, in _get_native_image
    (TWRC_XFERDONE, TWRC_CANCEL))
  File "C:\Python34\lib\site-packages\twain.py", line 1452, in _call
    return self._sm._call(self._id, dg, dat, msg, buf, expected_returns)
  File "C:\Python34\lib\site-packages\twain.py", line 2097, in _call
    raise exc
twain.excTWCC_SEQERROR

So I don't know what is the problem and differences the for and while loop? Do you know why ?

@denisenkom
Copy link
Owner

Here you should break the loop if count is zero

@denisenkom
Copy link
Owner

I added some docs: http://pytwain.readthedocs.org/en/latest/twain.html#twain.Source.xfer_image_natively

I renamed XferImageNatively to xfer_image_natively. XferImageNatively is now deprecated but it still aliased to xfer_image_natively for now.

@yazilimci1
Copy link
Author

Thank u denisenkom for your help . I ' ll try it .

@Dhruv00710
Copy link

import twain
import uuid
sm = twain.SourceManager(0)
ss = sm.OpenSource()
ss.RequestAcquire(1,1)
for i in range(3):
try:
rv = ss.XferImageNatively()
if rv:
(handle, count) = rv
twain.DIBToBMFile(handle, '{}.bmp'.format(uuid.uuid4()))
else:
break
except:
print("Error")
and its get error like
Error
Error
Error
Exception ignored in: <function SourceManager.del at 0x035A2848>
Traceback (most recent call last):
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain.py", line 2066, in del
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain.py", line 2073, in close
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib_weakrefset.py", line 105, in pop
KeyError: 'pop from empty WeakSet'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants