API Reference¶
Scanner Discovery¶
- scanlib.list_scanners(*, timeout=15.0, cancel=None)¶
Return all available scanners on the current platform.
timeout controls how long (in seconds) to wait for scanner discovery. The default is
DISCOVERY_TIMEOUT(15 s).cancel, if given, is a
threading.Eventthat the caller can set from another thread to interrupt discovery early. When the event is set the function returns immediately with an empty list.The returned
Scannerobjects are lightweight — no device sessions are opened. UseScanner.open()(or the context-manager protocol) to start a session before scanning.
Scanner¶
- class scanlib.Scanner(name, vendor, model, backend, *, scanner_id=None, location=None, _backend_impl=None)¶
Represents a discovered scanner device.
Use
open()/close()(or the context-manager protocol) to start a session before callingscan().- Parameters:
- property name: str¶
Platform-specific device name.
On SANE this is the device URI (e.g.
escl:http://192.168.1.5:443/eSCL), on macOS and WIA it is a human-readable display name. Always populated.
- property id: str¶
Platform-opaque string that uniquely identifies this device.
Guaranteed unique across all scanners returned by a single
list_scanners()call. On SANE this is the device URI, on WIA the device ID, on macOS the device UUID.
- property vendor: str | None¶
Scanner manufacturer, or
Nonewhen not available.Populated on SANE and macOS;
Noneon WIA where WSD reports the driver vendor rather than the hardware manufacturer.
- property model: str | None¶
Scanner model string, or
Nonewhen not available.Populated on SANE only;
Noneon macOS and WIA.
- property location: str | None¶
Free-form location string associated with the device.
Populated from
ICDevice.locationDescriptionon macOS, or from the mDNSnoteTXT record on Linux/Windows. ReturnsNonewhen no location is available.
- property sources: list[SourceInfo]¶
Available scan sources and their capabilities.
Each entry is a
SourceInfowithtype,resolutions,color_modes, andmax_scan_area. Only populated afteropen().The first entry is the scanner’s primary source (typically flatbed). When
scan()orscan_pages()is called without an explicit source, the first entry is used for parameter validation.
- property defaults: ScannerDefaults | None¶
Default settings and supported values detected from the device.
Returns
Noneif the backend could not determine defaults. Only available afteropen().
- abort()¶
Abort an in-progress scan.
Safe to call from any thread, even when no scan is running. The running
scan()orscan_pages()call will raiseScanAbortedshortly after this is called. The event is cleared automatically at the start of the next scan.- Return type:
- scan_pages(*, dpi=300, color_mode=ColorMode.COLOR, scan_area=None, source=None, progress=None, next_page=None, bw_threshold=128)¶
Scan and yield individual
ScannedPageobjects.Each page carries raw pixel data that can be inspected, encoded (via
ScannedPage.to_jpeg()/ScannedPage.to_png()), reordered, or later assembled into a PDF withbuild_pdf().When next_page is provided and the source is not a feeder, each page is yielded before the callback is invoked, so the caller can preview or process the page before deciding whether to continue scanning.
bw_threshold (0–255) controls the grayscale-to-BW cutoff when color_mode is
ColorMode.BW. Pixels with a value ≥ threshold become white; below become black. Default is 128.Parameters are the same as
scan()except that image_format and jpeg_quality are not applicable here.
- scan(*, dpi=300, color_mode=ColorMode.COLOR, scan_area=None, source=None, progress=None, next_page=None, image_format=None, jpeg_quality=85, bw_threshold=128)¶
Scan a document and return PDF bytes.
When source is
ScanSource.FEEDER, all pages in the document feeder are scanned. Otherwise a single page is scanned.When next_page is provided and the source is not a feeder, the callback is called after each page with the number of pages scanned so far. Return
Trueto scan another page orFalseto stop.image_format selects the encoding for page images inside the PDF:
ImageFormat.JPEG(smaller files) orImageFormat.PNG(lossless). When not specified, PNG is used for BW mode (since 1-bit packs much smaller than JPEG) and JPEG for everything else.jpeg_quality (1–100) controls JPEG compression quality; ignored when image_format is PNG.
bw_threshold (0–255) controls the grayscale-to-BW cutoff when color_mode is
ColorMode.BW. Default is 128.- Return type:
- Parameters:
Scanned Pages¶
- class scanlib.ScannedPage(data, width, height, color_mode)¶
A single scanned page with raw pixel data.
data contains raw pixel bytes with no header or wrapper — 1 byte per pixel for grayscale (
ColorMode.GRAY), 3 bytes per pixel (R, G, B) for color (ColorMode.COLOR), or 1-bit packed (MSB first) for black & white (ColorMode.BW).- rotate(degrees)¶
Rotate the page clockwise by 90, 180, or 270 degrees.
Returns a new
ScannedPagewith the rotated pixel data. For 90° and 270° rotations, width and height are swapped.- Return type:
- Parameters:
degrees (int)
- to_jpeg(quality=85)¶
Encode the page as JPEG and return the bytes.
Uses a platform-native encoder (ImageIO on macOS, WIC on Windows, libjpeg on Linux). quality ranges from 1 (smallest) to 100 (best). 1-bit BW pages are unpacked to 8-bit grayscale before encoding.
- scanlib.build_pdf(pages, *, dpi=300, color_mode=ColorMode.COLOR, image_format=None, jpeg_quality=85, bw_threshold=128)¶
Build a PDF from scanned pages.
pages is any iterable of
ScannedPageobjects — they may come directly fromScanner.scan_pages()or from a list that has been reordered, filtered, etc.image_format selects the encoding for page images inside the PDF:
ImageFormat.JPEG(smaller files) orImageFormat.PNG(lossless). When not specified, PNG is used for BW mode (since 1-bit packs much smaller than JPEG) and JPEG for everything else. jpeg_quality (1–100) controls JPEG compression; it is ignored when the format is PNG.bw_threshold (0–255) controls the grayscale-to-BW cutoff when color_mode is
ColorMode.BW. Default is 128.Returns a
ScannedDocumentcontaining the PDF bytes.- Return type:
- Parameters:
pages (Iterable[ScannedPage])
dpi (int)
color_mode (ColorMode)
image_format (ImageFormat | None)
jpeg_quality (int)
bw_threshold (int)
Scan Result¶
- class scanlib.ScannedDocument(data, page_count, width, height, dpi, color_mode)¶
Result of a scan operation.
datacontains PDF file bytes (one or more pages).widthandheightreflect the dimensions of the first page in pixels. Individual pages in the PDF may differ (e.g. after rotation).
Options & Configuration¶
- class scanlib.ScanOptions(dpi=300, color_mode=ColorMode.COLOR, scan_area=None, source=None, progress=None, bw_threshold=128)¶
Options for a scan operation.
- class scanlib.ColorMode(*values)¶
Color mode for scanning.
- COLOR = 'color'¶
- GRAY = 'gray'¶
- BW = 'bw'¶
- class scanlib.ImageFormat(*values)¶
Image encoding format used inside the PDF.
- PNG = 'png'¶
- JPEG = 'jpeg'¶
- class scanlib.ScanArea(x, y, width, height)¶
Scan region in 1/10 millimeters.
x and y are offsets from the top-left corner of the scanner bed. width and height are the dimensions of the region to scan.
- class scanlib.ScannerDefaults(dpi, color_mode, source)¶
Default settings detected from the device after opening.
- Parameters:
dpi (int)
color_mode (ColorMode)
source (ScanSource | None)
- class scanlib.SourceInfo(type, resolutions, color_modes, max_scan_area)¶
Capabilities of a single scan source.
Each
SourceInfobundles the supported resolutions, color modes, and maximum scan area for one scan source (flatbed or feeder). Access viaScanner.sourcesafter opening the device.
Exceptions¶
- class scanlib.ScanLibError¶
Base exception for scanlib.
- class scanlib.ScanError¶
An error occurred during scanning.
- class scanlib.ScanAborted¶
The scan was aborted before completion.
- class scanlib.FeederEmptyError¶
The document feeder has no pages to scan.
- class scanlib.ScannerNotOpenError¶
Operation requires an open scanner session.
- class scanlib.NoScannerFoundError¶
No scanner device was found.
- class scanlib.BackendNotAvailableError¶
The scanning backend for this platform is not installed.