• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

pyinsane2/H25-Jul-2018-5,3324,245

pyinsane2.egg-info/H03-May-2022-2625

tests/H25-Jul-2018-654514

AUTHORSH A D09-Jan-2017230 65

ChangeLogH A D25-Jul-20185.8 KiB153122

LICENSEH A D09-Jan-201734.3 KiB675553

MANIFEST.inH A D08-Feb-2018184 87

PKG-INFOH A D25-Jul-20181.2 KiB2625

README.mdH A D06-Jul-20188.8 KiB342234

setup.cfgH A D25-Jul-201865 85

setup.pyH A D06-Jul-20183.2 KiB10693

README.md

1# PyInsane 2
2
3## Description
4
5Python library to access and use image scanners.
6
7Support for:
8- [Sane](http://www.sane-project.org/) (Scanners on GNU/Linux, *BSD, MacOSX, etc)
9- WIA 2 (Windows Image Acquisition ; Scanners on Microsoft Windows >= Vista)
10
11It supports:
12- Flatbed
13- Automatic Document Feeder
14- While scanning, can provide chunks of the image for on-the-fly preview
15  (see [Paperwork](https://github.com/openpaperwork/paperwork/) for instance)
16- Python 2.7 (GNU/Linux only)
17- Python 3.x (GNU/Linux and Windows)
18
19Not tested but should work too:
20- Handheld image scanners
21
22
23## Dependencies
24
25On all platforms:
26- [Pillow](https://github.com/python-imaging/Pillow#readme) (if the abstraction layer is used)
27
28Platform specific:
29- GNU/Linux, *BSD, MacOSX, etc: [libsane](http://www.sane-project.org/)
30
31
32## Supported scanners
33
34In theory, all scanners supported by Sane or WIA should work.
35
36In practice, each driver tends to have
37[its own quirks](https://github.com/openpaperwork/paperwork/issues/533#issuecomment-262777789).
38Pyinsane [tries to include most of the workarounds you may need](/src/pyinsane2/__init__.py#L33).
39
40[There is a list of scanners known to work (or to have worked at some point)](doc/scanners.md).
41
42
43## Installation
44
45```sh
46# recommanded to get the latest stable version
47sudo pip3 install pyinsane2
48```
49
50or
51
52```sh
53# for the development version
54git clone https://github.com/openpaperwork/pyinsane.git
55cd pyinsane
56sudo make install  # will run 'python3 ./setup.py install'
57```
58
59Installation on GNU/Linux should work out-of-the-box.
60
61Installation on Windows will require Python, Visual C++ and WinDDK (see below
62for details).
63
64
65## Tests
66
67```sh
68make check  # check style + static analysis
69make test  # run tests
70```
71
72Tests require at least one scanner with a flatbed and an ADF (Automatic
73Document Feeder).
74
75If possible, they should be run with at least 2 scanners connected. The first
76that appear in "scanimage -L" must be the one with the ADF.
77
78For reference, my current setup is:
79- HP Officejet 4620 (Flatbed + ADF)
80- HP Deskjet 2050 J510 series (Flatbed)
81
82On GNU/Linux, you can simply enable the Sane backend 'test'.
83
84
85## Usage
86
87### Scanner detection
88
89```py
90import pyinsane2
91
92pyinsane2.init()
93try:
94	devices = pyinsane2.get_devices()
95	assert(len(devices) > 0)
96	device = devices[0]
97
98	print("I'm going to use the following scanner: %s" % (str(device)))
99	scanner_id = device.name
100finally:
101	pyinsane2.exit()
102```
103
104or if you already know its name/id:
105
106```py
107import pyinsane2
108
109pyinsane2.init()
110try:
111	device = pyinsane2.Scanner(name="somethingsomething")
112	print("I'm going to use the following scanner: %s" % (str(device)))
113finally:
114	pyinsane2.exit()
115```
116
117
118### Simple scan
119
120```py
121import pyinsane2
122
123pyinsane2.init()
124try:
125	devices = pyinsane2.get_devices()
126	assert(len(devices) > 0)
127	device = devices[0]
128	print("I'm going to use the following scanner: %s" % (str(device)))
129
130	pyinsane2.set_scanner_opt(device, 'resolution', [300])
131
132# Beware: Some scanners have "Lineart" or "Gray" as default mode
133# better set the mode everytime
134	pyinsane2.set_scanner_opt(device, 'mode', ['Color'])
135
136# Beware: by default, some scanners only scan part of the area
137# they could scan.
138	pyinsane2.maximize_scan_area(device)
139
140	scan_session = device.scan(multiple=False)
141	try:
142		while True:
143			scan_session.scan.read()
144	except EOFError:
145		pass
146	image = scan_session.images[-1]
147finally:
148	pyinsane2.exit()
149```
150
151See examples/scan.py for a more complete example.
152
153
154### Multiple scans using an automatic document feeder (ADF)
155
156```py
157import pyinsane2
158
159pyinsane2.init()
160try:
161	devices = pyinsane2.get_devices()
162	assert(len(devices) > 0)
163	device = devices[0]
164	print("I'm going to use the following scanner: %s" % (str(device)))
165
166	try:
167		pyinsane2.set_scanner_opt(device, 'source', ['ADF', 'Feeder'])
168	except PyinsaneException:
169		print("No document feeder found")
170		return
171
172# Beware: Some scanners have "Lineart" or "Gray" as default mode
173# better set the mode everytime
174	pyinsane2.set_scanner_opt(device, 'mode', ['Color'])
175
176# Beware: by default, some scanners only scan part of the area
177# they could scan.
178	pyinsane2.maximize_scan_area(device)
179
180	scan_session = device.scan(multiple=True)
181	try:
182		while True:
183			try:
184				scan_session.scan.read()
185			except EOFError:
186				print ("Got a page ! (current number of pages read: %d)"
187					% (len(scan_session.images)))
188	except StopIteration:
189		print("Document feeder is now empty. Got %d pages"
190			% len(scan_session.images))
191	for idx in range(0, len(scan_session.images)):
192		image = scan_session.images[idx]
193finally:
194	pyinsane2.exit()
195```
196
197
198### Scanner's options
199
200The options available depends on the backend and on the specific driver used.
201
202The WIA implementation emulates common Sane options ('tl-x', 'br-x', 'tl-y', 'br-y',
203'color', 'mode', 'source'). So you should use Sane options by default.
204
205See [the Sane documentation](http://www.sane-project.org/html/doc014.html) for the
206most common options.
207
208Beware options casing can change between WIA and Sane implementation !
209You should use ```pyinsane2.set_scanner_opt()``` whenever possible.
210
211
212You can access the option values with:
213
214```py
215device.options['option_name'].value
216```
217
218You can set the option values with:
219
220```py
221device.options['option_name'].value = new_value
222# or use the helper:
223pyinsane2.set_scanner_opt(
224    device, 'option_name',
225    ['possible_new_value_1', 'possible_new_value_2']
226)
227```
228
229You can get the constraint (accepted values) with:
230
231```py
232device.options['option_name'].constraint
233```
234
235Constraints are usually:
236
237* None : unknown constraints / no constraint
238* tuple : ```(min_value, max_value)```
239* list : possible values (Ex: ```['Flatbed', 'Feeder']``` or ```[75, 150, 300]```)
240
241
242### Note regarding the Sane implementation
243
244When using the Sane API as is, some issues with some Sane drivers can become
245obvious in complex programs (uninitialized memory bytes, segfault, etc).
246You can get corrupted images or even crash your program.
247
248This module works around issues like the following one by using a dedicated
249process for scanning:
250
251<table border="0">
252	<tr>
253		<td>
254			<img src="https://raw.githubusercontent.com/openpaperwork/pyinsane/master/doc/sane_driver_corrupted_mem.png" alt="corrupted scan" width="359" height="300" />
255		</td>
256		<td>
257			--&gt;
258		</td>
259		<td>
260			<img src="https://raw.githubusercontent.com/openpaperwork/pyinsane/master/doc/sane_proc_workaround.png" alt="scan fine" width="352" height="308" />
261		</td>
262	</tr>
263</table>
264
265(see [this comment for details](https://github.com/openpaperwork/paperwork/issues/486#issuecomment-233925642))
266
267When ```pyinsane2.init()``` is called, it will create 2 Unix pipes (FIFO)
268in your temporary directory and a dedicated process. To avoid forking
269other file descriptors from your program, you should initialize pyinsane2
270as soon as possible.
271
272Building requires nothing except Python. Libsane is loaded dynamically using ```ctypes```.
273
274
275### Note regarding the WIA 2 implementation
276
277#### Build
278
279Build requires:
280
281* Either Python 3.4 + Windows SDK 7.1 (Visual C++ 2010) + Windows DDK (aka WDK)
282* Or Python 3.5 + Visual C++ 2016 + Windows DDK (aka WDK) (included in Visual Studio 2016)
283
284(see [the Python wiki for more information](https://wiki.python.org/moin/WindowsCompilers))
285
286You must define the following environment values before calling ```python setup.py install```:
287
288- WINDDK_INCLUDE_DIR (default value: c:\winddk\7600.16385.1\inc\atl71)
289- WINDDK_LIB_DIR (default value: c:\winddk\7600.16385.1\lib\ATL\amd64)
290
291
292#### Usage
293
294WIA provides one WiaItem2 by possible source (Flatbed, ADF, etc). And each of
295these items has its own properties.
296
297To make the model consistent with Sane, all the properties have been merged in
298the same list. When you update a property specific to sources, it is updated
299on all the WIAItem2. The update is considered successful if it worked at
300least on one.
301
302Some properties are emulated to make the API behavior consistent with the Sane
303implementation. The WIA implementation emulates common Sane options
304('tl-x', 'br-x', 'tl-y', 'br-y', 'color', 'mode', 'source').
305If your program must be cross-platform, you are strongly advised to use
306these emulated options instead of the WIA ones ('xpos', 'ypos', 'xextent',
307'yextent', etc).
308
309
310### Other examples
311
312The folder 'examples' contains more detailed examples.
313For instance, examples/scan.py shows how to get pieces of a scan as it goes.
314
315To run one of these scripts, run:
316
317	python -m examples.[script] [args]
318
319For instance
320
321	python -m examples.scan toto.png
322
323
324## Contact
325
326* [Mailing-list](https://github.com/openpaperwork/paperwork/wiki/Contact#mailing-list)
327* [Bug tracker](https://github.com/openpaperwork/pyinsane/issues/)
328
329
330## Application that uses Pyinsane
331
332* [Paperwork](https://github.com/openpaperwork/paperwork#readme)
333
334If you know of any other applications that use Pyinsane, please
335[tell us](https://github.com/openpaperwork/paperwork/wiki/Contact#mailing-list) :-)
336
337
338## Licence
339
340GPL v3
3412012-2016(c) Jerome Flesch (<jflesch@gmail.com>)
342