-
Notifications
You must be signed in to change notification settings - Fork 19
/
sanedoc.txt
362 lines (234 loc) · 7.85 KB
/
sanedoc.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
python-sane documentation
*************************
The sane module is a Python interface to the SANE (Scanner Access Now
Easy) library, which provides access to various raster scanning
devices such as flatbed scanners and digital cameras. For more
information about SANE, consult the SANE website at www.sane-
project.org. Note that this documentation doesn't duplicate all the
information in the SANE documentation, which you must also consult to
get a complete understanding.
This module has been originally developed by A.M. Kuchling, it is
currently maintained by Sandro Mani.
* Indices
* Reference
* Example
Indices
=======
* *Index*
* *Search Page*
Reference
=========
sane.init()
Initialize sane.
Returns:
A tuple "(sane_ver, ver_maj, ver_min, ver_patch)".
Raises _sane.error:
If an error occurs.
sane.get_devices(localOnly=False)
Return a list of 4-tuples containing the available scanning
devices. If *localOnly* is *True*, only local devices will be
returned. Each tuple is of the format "(device_name, vendor, model,
type)", with:
* *device_name* -- The device name, suitable for passing to
"sane.open()".
* *vendor* -- The device vendor.
* *mode* -- The device model vendor.
* *type* -- the device type, such as ""virtual device"" or
""video camera"".
Returns:
A list of scanning devices.
Raises _sane.error:
If an error occurs.
sane.open(devname)
Open a device for scanning. Suitable values for devname are
returned in the first item of the tuples returned by
"sane.get_devices()".
Returns:
A "SaneDev" object on success.
Raises _sane.error:
If an error occurs.
sane.exit()
Exit sane.
class class sane.SaneDev(devname)
Class representing a SANE device. Besides the functions documented
below, the class has some special attributes which can be read:
* *devname* -- The scanner device name (as passed to
"sane.open()").
* *sane_signature* -- The tuple "(devname, brand, name, type)".
* *scanner_model* -- The tuple "(brand, name)".
* *opt* -- Dictionary of options.
* *optlist* -- List of option names.
* *area* -- Scan area.
Furthermore, the scanner options are also exposed as attributes,
which can be read and set:
print(scanner.mode)
scanner.mode = 'Color'
An "Option" object for a scanner option can be retrieved via
"__getitem__()", i.e.:
option = scanner['mode']
arr_scan()
Convenience method which calls "SaneDev.start()" followed by
"SaneDev.arr_snap()".
arr_snap()
Read image data and return a 3d numpy array of the shape
"(nbands, width, height)".
Returns:
A "numpy.array" object.
Raises:
* **_sane.error** -- If an error occurs.
* **RuntimeError** -- If *numpy* cannot be imported.
cancel()
Cancel an in-progress scanning operation.
close()
Close the scanning device.
fileno()
Returns:
The file descriptor for the scanning device, if any.
Raises _sane.error:
If an error occurs.
get_options()
Returns:
A list of tuples describing all the available options.
get_parameters()
Returns a 5-tuple holding all the current device settings:
"(format, last_frame, (pixels_per_line, lines), depth,
bytes_per_line)"
* *format* -- One of ""grey"", ""color"", ""red"",
""green"", ""blue"" or ""unknown format"".
* *last_frame* -- Whether this is the last frame of a
multi frame
image.
* *pixels_per_line* -- Width of the scanned image.
* *lines* -- Height of the scanned image.
* *depth* -- The number of bits per sample.
* *bytes_per_line* -- The number of bytes per line.
Returns:
A tuple containing the device settings.
Raises _sane.error:
If an error occurs.
Note: Some backends may return different parameters depending on
whether "SaneDev.start()" was called or not.
multi_scan()
Returns:
A "_SaneIterator" for ADF scans.
scan()
Convenience method which calls "SaneDev.start()" followed by
"SaneDev.snap()".
snap(no_cancel=False)
Read image data and return a "PIL.Image" object. An RGB image is
returned for multi-band images, an L image for single-band
images. "no_cancel" is used for ADF scans by "_SaneIterator".
Returns:
A "PIL.Image" object.
Raises:
* **_sane.error** -- If an error occurs.
* **RuntimeError** -- If *PIL.Image* cannot be imported.
start()
Initiate a scanning operation.
Throws _sane.error:
If an error occurs, for instance if an option is set to an
invalid value.
class class sane.Option(args, scanDev)
Class representing a SANE option. These are returned by a
"SaneDev.__getitem__()" lookup of an option on the device, i.e.:
option = scanner["mode"]
The "Option" class has the following attributes:
* *index* -- Number from "0" to "n", giving the option number.
* *name* -- A string uniquely identifying the option.
* *title* -- Single-line string containing a title for the
option.
* *desc* -- A long string describing the option, useful as a
help
message.
* *type* -- Type of this option: "TYPE_BOOL", "TYPE_INT",
"TYPE_STRING", etc.
* *unit* -- Units of this option. "UNIT_NONE", "UNIT_PIXEL",
etc.
* *size* -- Size of the value in bytes.
* *cap* -- Capabilities available: "CAP_EMULATED",
"CAP_SOFT_SELECT",
etc.
* *constraint* -- Constraint on values. Possible values:
* None : No constraint
* "(min,max,step)" : Range
* list of integers or strings: listed of permitted values
is_active()
Returns:
Whether the option is active.
is_settable()
Returns:
Whether the option is settable.
class class sane._SaneIterator(device)
Iterator for ADF scans.
Example
=======
#!/usr/bin/env python
from __future__ import print_function
import sane
import numpy
from PIL import Image
#
# Change these for 16bit / grayscale scans
#
depth = 8
mode = 'color'
#
# Initialize sane
#
ver = sane.init()
print('SANE version:', ver)
#
# Get devices
#
devices = sane.get_devices()
print('Available devices:', devices)
#
# Open first device
#
dev = sane.open(devices[0][0])
#
# Set some options
#
params = dev.get_parameters()
try:
dev.depth = depth
except:
print('Cannot set depth, defaulting to %d' % params[3])
try:
dev.mode = mode
except:
print('Cannot set mode, defaulting to %s' % params[0])
try:
dev.br_x = 320.
dev.br_y = 240.
except:
print('Cannot set scan area, using default')
params = dev.get_parameters()
print('Device parameters:', params)
#
# Start a scan and get a PIL.Image object
#
dev.start()
im = dev.snap()
im.save('test_pil.png')
#
# Start another scan and get a numpy array object
#
# Initiate the scan and get a numpy array
dev.start()
arr = dev.arr_snap()
print("Array shape: %s, size: %d, type: %s, range: %d-%d, mean: %.1f, stddev: "
"%.1f" % (repr(arr.shape), arr.size, arr.dtype, arr.min(), arr.max(),
arr.mean(), arr.std()))
if arr.dtype == numpy.uint16:
arr = (arr / 255).astype(numpy.uint8)
if params[0] == 'color':
im = Image.frombytes('RGB', arr.shape[1:], arr.tostring(), 'raw', 'RGB', 0,
1)
else:
im = Image.frombytes('L', arr.shape[1:], arr.tostring(), 'raw', 'L', 0, 1)
im.save('test_numpy.png')
#
# Close the device
#
dev.close()