我在 Python - Django 中使用 Pyinsane 2 通过网络 HP 打印机扫描图像。它可以正确检测设备。但是在给出 scan_session = device.scan(multiple=False) 时,它给出了 StopIteration 错误。
def scan_process(request): print('Scannned'); pyinsane2.init() try: devices = pyinsane2.get_devices() assert (len(devices) > 0) device = devices[0] print("我将使用以下扫描仪:%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("我将使用以下扫描仪:%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()