I use Pyinsane 2 in Python - Django to scan a image via a network HP Printer. it detects the Device and all correctly. but while give scan_session = device.scan(multiple=False) it gives StopIteration error.
def scan_process(request): print('Scannned'); pyinsane2.init() try: devices = pyinsane2.get_devices() assert (len(devices) > 0) device = devices[0] print("I'm going to use the following scanner: %s" % (str(device)))
try:
pyinsane2.set_scanner_opt(device, 'source', ['ADF', 'Feeder'])
except PyinsaneException:
print("No document feeder found")
pyinsane2.set_scanner_opt(device, 'mode', ['Gray'])
pyinsane2.maximize_scan_area(device)
scan_session = device.scan(multiple=False)
try:
while True:
try:
scan_session.scan.read()
except EOFError:
print("Got a page ! (current number of pages read: %d)" % (len(scan_session.images)))
except StopIteration:
print("Document feeder is now empty. Got %d pages" % len(scan_session.images))
for idx in range(0, len(scan_session.images)):
image = scan_session.images[idx]
image.save("teste_%d.bmp" % idx)
finally:
pyinsane2.exit()
def scan_process(request): print('Scannned'); pyinsane2.init() try: devices = pyinsane2.get_devices() assert (len(devices) > 0) device = devices[0] print("I'm going to use the following scanner: %s" % (str(device)))
try:
# pyinsane2.set_scanner_opt(device, 'source', ['ADF', 'Feeder'])
pyinsane2.set_scanner_opt(device, 'source', ['ADF', 'Feeder'])
except PyinsaneException:
print("No document feeder found")
# return
# Beware: Some scanners have "Lineart" or "Gray" as default mode
# better set the mode everytime
pyinsane2.set_scanner_opt(device, 'mode', ['Gray'])
# Beware: by default, some scanners only scan part of the area
# they could scan.
pyinsane2.maximize_scan_area(device)
scan_session = device.scan(multiple=False)
try:
while True:
try:
scan_session.scan.read()
except EOFError:
print("Got a page ! (current number of pages read: %d)" % (len(scan_session.images)))
except StopIteration:
print("Document feeder is now empty. Got %d pages" % len(scan_session.images))
for idx in range(0, len(scan_session.images)):
image = scan_session.images[idx]
image.save("teste_%d.bmp" % idx)
finally:
pyinsane2.exit()
An immediate StopIteration means that the scanner reported having no sheet at all in the feeder. If you are using a flatbed scanner .. then it's a bug (but it's always hard to say if the bug comes from the driver or Pyinsane2 itself).
Note that I don't maintain Pyinsane2 anymore. I suggest you try using Libinsane instead: https://gitlab.gnome.org/World/OpenPaperwork/libinsane .