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.
-
Will this be on a Windows computer or Linux? – Jason Sperske Mar 26 '13 at 06:56
-
I want to use it cross platform – Aryan Mar 26 '13 at 07:11
5 Answers
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()

- 29,816
- 8
- 73
- 124
-
-
It's been a while since I've utilized this answer but doesn't 64 bit Windows include a 32 bit software emulator? – Jason Sperske Jan 01 '16 at 17:48
-
64-bit Windows does support running 32-bit software. My guess is that TWAIN interacts with the system at a low enough level that the architecture is a significant detail. – Moshe Jan 01 '16 at 19:01
-
@JasonSperske I don't understand how to install python-imagescanner on windows with [documentation](https://code.google.com/p/imagescanner/wiki/GettingStarted) – Cahit Yıldırım Jan 08 '16 at 00:59
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()

- 907
- 12
- 28
-
There is something wrong with your link, I think it should be this one: https://sites.tntech.edu/renfro/2009/09/03/capturing-an-image-from-a-wia-compatible-digital-camera/ – Albert Oct 09 '20 at 11:57
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!!

- 121
- 2
- 6
Based on the @Marcelo Gazzola, here's a cleaned version with parameters (tested on windows10, canon lide70):
WIA_COM = "WIA.CommonDialog"
WIA_IMG_FORMAT_PNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
def acquire_image_wia(strDstFilename,rectToScan=(0,0,210,297),nBitsPerPixel=24,nResolution = 300):
"""
scan an image and save it to disk
- strDstFilename: pathfilenale of destination
- rectToScan: rect to grab in milimeters (default: A4)
- nBitsPerPixel: 1: N&B, 8: gray, 24: color
- nResolution: resolution in DPI (300, 600, ...)
"""
wia = win32com.client.Dispatch(WIA_COM) # wia is a CommonDialog object
dev = wia.ShowSelectDevice()
scanner = dev.Items[0]
rMagicCoef = 1.17 # looks like a magic, tuned for my canon lide 70. perhaps some DotToInch explanations...
r = []
for x in rectToScan:
r.append(int(x*10*rMagicCoef*nResolution/300))
for p in scanner.Properties:
if p.Name == "Horizontal Start Position":
p.value = r[0]
if p.Name == "Vertical Start Position":
p.value = r[1]
if p.Name == "Horizontal Extent":
p.value = r[2]
if p.Name == "Vertical Extent":
p.value = r[3]
if p.Name == "Bits Per Pixel":
p.value = nBitsPerPixel
if p.Name == "Horizontal Resolution":
p.value = nResolution
if p.Name == "Vertical Resolution":
p.value = nResolution
image=scanner.Transfer(WIA_IMG_FORMAT_PNG)
if os.path.exists(strDstFilename):
os.remove(strDstFilename)
image.SaveFile(strDstFilename)

- 2,462
- 20
- 26