Question

I want to write program with python which can get input from scanner and save as jpg. I don't have any idea how to start. please help.

Was it helpful?

Solution 2

In Windows, the module you will want to look into is called the Python TWAIN module, while in Linux (and I think Mac) you should look into pysane.

Now that I am digging into this it looks like there is a project called python-imagescanner that tries to wrap these two approaches into a common library,

From imagescanner's documentation

Getting access to a scanner device:

 from imagescanner import ImageScanner

 # instantiate the imagescanner obj 
 iscanner = ImageScanner()

 # get all available devices
 scanners = iscanner.list_scanners()

 # choose one of the devices
 scanner = scanners[0]

 # scan your file (returns a PIL object)
 scanner.scan()

OTHER TIPS

Six years have passed and I came here today looking for the answer to the same question.

Pysane and python-imagescanner in the currently accepted answer are unfortunately no longer active, but after som further searching I found libinsane which seems to be a better option nowadays.

Me too, six years later searching and I found this Capturing an Image from a WIA-compatible Digital Camera that really helps. It uses win32com.client.

It worked with my scanner Cannon DR-C240.

Code:

import win32com.client, time, os

WIA_COM = "WIA.CommonDialog"

WIA_DEVICE_UNSPECIFIED = 0
WIA_DEVICE_CAMERA = 2

WIA_INTENT_UNSPECIFIED = 0

WIA_BIAS_MIN_SIZE = 65536
WIA_BIAS_MAX_QUALITY = 65536

WIA_IMG_FORMAT_PNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"

WIA_COMMAND_TAKE_PICTURE="{AF933CAC-ACAD-11D2-A093-00C04F72DC3C}"

def acquire_image_wia():
    wia = win32com.client.Dispatch(WIA_COM) # wia is a CommonDialog object
    dev = wia.ShowSelectDevice()
    for command in dev.Commands:
        if command.CommandID==WIA_COMMAND_TAKE_PICTURE:
            foo=dev.ExecuteCommand(WIA_COMMAND_TAKE_PICTURE)

    i=1
    for item in dev.Items:
        if i==dev.Items.Count:
            image=item.Transfer(WIA_IMG_FORMAT_PNG)
            break
        i=i+1

    fname = 'wia-test.png'
    if os.path.exists(fname):
        os.remove(fname)
    image.SaveFile(fname)

os.chdir("c:/temp")
acquire_image_wia()

Just mentioning other approach here in case anyone need simple approach having little code otherwise you can go with @Marcelo's answer above.

The idea is to use Windows Fax and Scan for scanning document. Here is the code in python for same.

import os
os.system("WFS")

It will open Windows Fax and Scan for you. Now you can scan your document.

Good Luck!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top