16-bit color images with pyinsane

377 views Asked by At

pyinsane's scan sessions return a list of 8-bit PIL images by default. This is true, even when the scan has been done in 16-bit mode (for instance using the transparency unit). Is there any way to get 16-bit images (I suppose PIL does not support that) or the original raw data out of pyinsane?

Here is the sample code I am currently using and getting images with 8 bits colour depth:

import pyinsane.abstract as pyinsane
device = pyinsane.get_devices()[0]

device.options['resolution'].value = 1200
device.options['mode'].value = 'Color'
device.options['source'].value = 'Transparency Unit'

scan_session = device.scan(multiple=False)
try:
    while True:
        scan_session.scan.read()
except EOFError:
    pass
image = scan_session.images[0]
1

There are 1 answers

0
Jerome Flesch On BEST ANSWER

You're right, this is a limitation from Pillow (PIL). You can actually see the conversion from raw to PIL Image here : https://github.com/openpaperwork/pyinsane/blob/master/pyinsane2/sane/abstract.py#L161

If you really need this extra data, I guess the only option is to use the Sane API directly and do your own conversions:

import pyinsane2.sane.rawapi

pyinsane.sane.rawapi.sane_init()
(...)
pyinsane.sane.rawapi.sane_exit()

Unfortunately, doing this will make you loose the Windows portability (WIA support), and this part of Pyinsane is not documented at all. However, pyinsane.sane.rawapi provides the Sane C API with only minor transformations to make it more Pythony-friendly. So I guess you can just refer to the Sane documentation for informations : http://www.sane-project.org/html/doc009.html .