Scanning from same-model scanners with WIA (Windows Image Acquisition)

165 Views Asked by At

I am trying to use the WIA framework (in Python) to scan from two scanners of the exact same brand and model and it does not work properly.

The problem is that, even if I properly specify each device's UDID, it's always the same (default?) scanner that ends up being used. The second one is always ignored.

In my attempts to understand what is going on, I tried using Windows Fax & Scan and I get the same behaviour. This makes me think that there is a bug in the WIA code (which I'm assuming Windows Fax & Scan rely on).

This is the code I'm using in Python:

import win32com.client, time, os

# These are the UDIDs for my two devices
# SCANNER_UDID = r'{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0003'
SCANNER_UDID = r"{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0004"

WIA_IMG_FORMAT_PNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
WIA_COMMAND_TAKE_PICTURE = "{AF933CAC-ACAD-11D2-A093-00C04F72DC3C}"

def acquire_image_wia():
    
    # Get a WIA device manager
    device_manager = win32com.client.Dispatch("WIA.DeviceManager")

    # Get device by UDID
    device_info = device_manager.DeviceInfos(SCANNER_UDID)
    device = device_info.Connect()

    # Print the info so I know I have the correct device
    for prop in device_info.Properties: print(prop.Name + " : " + str(prop.Value))

    # Snap picture
    # device.ExecuteCommand(WIA_COMMAND_TAKE_PICTURE) # this doesn't work for some weird reason
    for command in device.Commands:
        if command.CommandID == WIA_COMMAND_TAKE_PICTURE:
            device.ExecuteCommand(WIA_COMMAND_TAKE_PICTURE)

    # Transfer last image (doesn't actually use PNG format, but this still is valid syntax).
    image = device.Items[device.Items.count].Transfer(WIA_IMG_FORMAT_PNG)

    # Save into file
    fname = 'wia-test.jpg'
    if os.path.exists(fname): os.remove(fname)
    image.SaveFile(fname)

Ideas?

P.S. I tried doing the same thing in Linux with scanimage and it worked fine.

0

There are 0 best solutions below