denisenkom / pytwain

TWAIN library using ctypes bindings
Other
46 stars 23 forks source link

TypeError: string argument expected, got 'bytes' when file name is None #16

Closed PEHDOM closed 7 years ago

PEHDOM commented 7 years ago

I am trying this code out below.

import twain
sm = twain.SourceManager(0)
ss = sm.OpenSource()
ss.RequestAcquire(0,0)
rv = ss.XferImageNatively()
if rv:
    (handle, count) = rv
    result = twain.DIBToBMFile(handle)

and got error:


Traceback (most recent call last):
  File "Y:\My_scripts\scan\scanner.py", line 27, in <module>
     result = twain.DIBToBMFile(handle) 
  File "Y:\My_scripts\scan\twain.py", line 2302, in DIBToBMFile
    return _dib_write(handle, path, _GlobalLock, _GlobalUnlock)
  File "Y:\My_scripts\scan\twain.py", line 2247, in _dib_write
    _write_bmp(f)
  File "Y:\My_scripts\scan\twain.py", line 2232, in _write_bmp
    f.write(b'BM')
TypeError: string argument expected, got 'bytes'

For fix this, you need change f = io.StringIO() to f = io.BytesIO() in twain._dib_write function.

PEHDOM commented 7 years ago

Sorry, I see bug fixed 2 month ago in Git. At fist I use pytwain from https://pypi.python.org/pypi/pytwain. It have this bug.