0

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()
Ramesh
  • 51
  • 7

1 Answers1

0

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 .

  • How to get libinsane ? is it available in pypi? – Ramesh Aug 10 '19 at 11:48
  • Now that stop iteration error rectified. I now got the error pyinsane2.sane.rawapi.SaneStatus data is invalid. This is now a ADF Scanner. – Ramesh Aug 10 '19 at 11:50
  • Libinsane is mainly a C library with bindings for many languages (Python included): https://doc.openpaper.work/libinsane/latest/libinsane/install.html – Jerome Flesch Aug 10 '19 at 15:50
  • Regarding "SaneStatus data is invalid" is usually due to a Sane driver bug. I got it quite often with Brother scanners. Usually shutting them down, restarting them and restart/reloading the program using Pyinsane2 did the trick. – Jerome Flesch Aug 10 '19 at 15:52
  • SaneStatus data is invalid" is now corrected using pyinsane2.set_scanner_opt(device, "source", ["ADF Front"]) . Thanks. – Ramesh Aug 12 '19 at 05:59
  • Totally i Scanned 4 Pages . The First page only got saved in a clear manner. The rest 3 will be Blur only always. – Ramesh Aug 12 '19 at 06:00
  • I assume your scanner works correctly with other applications like SimpleScan ? Also, I would be interrested in a test scan report using IronScanner : https://openpaper.work/fr/scanner_db/#contribute (it will scan using your scanner then send a whole report to openpaper.work). With some luck it should help me understand what's wrong. (note that IronScanner uses Libinsane and not Pyinsane anymore) – Jerome Flesch Aug 12 '19 at 12:34
  • I downloaded the Ironscanner from git. I am unable to use it in Pycharm Community Edition. The Terminal Cmd also not Successful. i have some questions. 1) Why Libinsane is Not in pypi packages? 2) I use HP and Kodak Scanner . In HP It Works fine from Python and all pages is clearly scanned. but in Kodak i2000 only first image got clearly scanned and remaining is very blur. – Ramesh Aug 13 '19 at 07:02
  • You can build IronScanner from sources if you like, but there are also binary versions available for Linux and Windows if you prefer. Regarding Pypi, Libinsane is not on Pypi because it is not a Python module but a C library with Python bindings generated on-the-fly by GObject Introspection (the same method is used when you use GTK with Python for instance). Regarding your scanners, I had an HP scanner but no Kodak one, so unless you submit a scan test report with IronScanner, I won't be able to help you. – Jerome Flesch Aug 13 '19 at 08:20
  • Also, did you try your Kodak scanner with another application like Simple-Scan for example ? – Jerome Flesch Aug 13 '19 at 08:23
  • Yes, using Simple Scan for Kodak - Its Works Perfectly. – Ramesh Aug 13 '19 at 14:53
  • OK, Help to Refer the Libinsane to my Python Project. – Ramesh Aug 14 '19 at 09:55
  • The most straightforward way: 1) Install the build dependencies of Libinsane ( https://doc.openpaper.work/libinsane/latest/libinsane/install.html ) ; 2) Build and install it system-wide (`make install PREFIX=/usr`) ; 3) Use it from Python (`from gi.repository import Libinsane` ; https://doc.openpaper.work/libinsane/latest/libinsane/howto_python.html). – Jerome Flesch Aug 14 '19 at 12:07