ocracy.util#
Image-input normalization and small shared helpers.
Different OCR backends want their input in different forms: a filesystem path
(Tesseract, PaddleOCR), raw bytes (most cloud REST APIs), a PIL image, or a
numpy array (deep-learning engines). Callers, meanwhile, want to pass whatever
they have — a path, an http(s) URL, bytes, a PIL image, or an array. This
module bridges the two with a handful of converters that lazily import
Pillow / numpy / urllib only when actually exercised, so import ocracy stays
dependency-free.
The key converters:
load_image_bytes()— anything -> encoded imagebytes(for REST APIs).ensure_file_path()— anything -> a path on disk (writing a temp file for in-memory inputs); pair withcleanup_temp()or useimage_path().image_path()— a context manager yielding a path and cleaning up.to_pil()/to_numpy()— anything -> a PIL image / numpy array.
Functions
|
Classify an image input as |
|
True if |
|
Return encoded image |
|
Return |
|
Delete |
|
Context manager yielding a filesystem path for |
|
Convert any supported input into a PIL |
|
Convert any supported input into a numpy array (lazy import). |
|
Import |
- ocracy.util.check_import(module_name, *, install_hint, feature='this')[source]#
Import
module_nameor raise a friendly, actionable ImportError.Centralizes the “you need to
pip install X” guidance so adapters and converters don’t each hand-roll it.
- ocracy.util.classify_input(image)[source]#
Classify an image input as
url/path/bytes/pil/numpy.Decided structurally (and via duck typing for PIL/numpy) so we never import Pillow or numpy just to look at the input.
- Return type:
str
- ocracy.util.cleanup_temp(path, is_temp)[source]#
Delete
pathiffis_temp(the flag returned byensure_file_path()).- Return type:
- ocracy.util.ensure_file_path(image, *, suffix='.png')[source]#
Return
(path, is_temp)for any supported input.Existing on-disk paths are returned untouched (
is_temp=False). In-memory inputs (bytes/URL/PIL/numpy) are written to a temp file (is_temp=True); the caller is responsible for cleanup (usecleanup_temp(), or prefer theimage_path()context manager).- Return type:
Tuple[str, bool]
- ocracy.util.image_path(image, *, suffix='.png')[source]#
Context manager yielding a filesystem path for
image, cleaning up temps.Example:
with image_path(pil_image) as p: text = pytesseract.image_to_string(p)
- Return type:
Iterator[str]