pythonpython-os

Is there a way to send a scan request to a printer using python OS library?


I'm currently building a text based console application to manage printing, scanning, etc but as usuall i have stumped my self and have had to come to stackoverflow for help. Any help is appreciated.

I have imported the os library.

import os

But unsure of a function to scan.


Solution

  • Scanning

    You can check this library called LibInsane to scan from printers using Python. But to install the library you're going to have to build the library from the source.

    Install the LibInsane library

    How to use Libinsane in a Python program

    command line

    The latter 2 commands must be run inside the libinsane directory. You can navigate to the example sample projects and run:

    python list_device.py
    
    python scan.py outputfilename.png
    
    python script

    Modify the .py file paths and the output image path before running it

    import os
    
    # for listing devices
    devices = os.system("python list_device.py")
    
    # for listing devices
    scanned_doc = os.system("python scan.py outputfilename.png")
    

    This is what the scan.py file contains

    def scan(source, output_file):
        session = source.scan_start()
     
        try:
            page_nb = 0
            while not session.end_of_feed() and page_nb < 20:
                # Do not assume that all the pages will have the same size !
                scan_params = session.get_scan_parameters()
                print("Expected scan parameters: {} ; {}x{} = {} bytes".format(
                      scan_params.get_format(),
                      scan_params.get_width(), scan_params.get_height(),
                      scan_params.get_image_size()))
                total = scan_params.get_image_size()
     
                img = []
                r = 0
                if output_file is not None:
                    out = output_file.format(page_nb)
                else:
                    out = None
                print("Scanning page {} --> {}".format(page_nb, out))
                while not session.end_of_page():
                    data = session.read_bytes(128 * 1024)
                    data = data.get_data()
                    img.append(data)
                    r += len(data)
                    print("Got {} bytes => {}/{} bytes".format(
                        len(data), r, total)
                    )
                img = b"".join(img)
                print("Got {} bytes".format(len(img)))
                if out is not None:
                    print("Saving page as {} ...".format(out))
                    if scan_params.get_format() == Libinsane.ImgFormat.RAW_RGB_24:
                        img = raw_to_img(scan_params, img)
                        img.save(out, format="PNG")
                    else:
                        print("Warning: output format is {}".format(
                            scan_params.get_format()
                        ))
                        with open(out, 'wb') as fd:
                            fd.write(img)
                page_nb += 1
                print("Page {} scanned".format(page_nb))
            if page_nb == 0:
                print("No page in feeder ?")
        finally:
            session.cancel()
    

    You can read more from the official documentation here

    Printing

    To print from a specific printer using python, check out this answer here https://stackoverflow.com/a/76981889/13520498