mirror of
https://github.com/ocrmypdf/OCRmyPDF.git
synced 2026-05-04 12:48:02 -04:00
typing: fix Pillow usage, leptonica
This commit is contained in:
@@ -521,13 +521,12 @@ def create_ocr_image(image: Path, page_context: PageContext):
|
||||
# be None)
|
||||
bbox = [float(v) for v in textarea]
|
||||
xyscale = tuple(float(coord) / 72.0 for coord in im.info['dpi'])
|
||||
pixcoords = [
|
||||
pixcoords = (
|
||||
bbox[0] * xyscale[0],
|
||||
im.height - bbox[3] * xyscale[1],
|
||||
bbox[2] * xyscale[0],
|
||||
im.height - bbox[1] * xyscale[1],
|
||||
]
|
||||
pixcoords = [int(round(c)) for c in pixcoords]
|
||||
)
|
||||
log.debug('blanking %r', pixcoords)
|
||||
draw.rectangle(pixcoords, fill=white)
|
||||
# draw.rectangle(pixcoords, outline=pink)
|
||||
|
||||
@@ -22,6 +22,7 @@ from functools import lru_cache
|
||||
from io import BytesIO, UnsupportedOperation
|
||||
from os import fspath
|
||||
from tempfile import TemporaryFile
|
||||
from typing import ContextManager, Type
|
||||
from warnings import warn
|
||||
|
||||
from ocrmypdf.exceptions import MissingDependencyError
|
||||
@@ -85,7 +86,7 @@ except ffi.error as e:
|
||||
) from e
|
||||
|
||||
|
||||
class _LeptonicaErrorTrap_Redirect:
|
||||
class _LeptonicaErrorTrap_Redirect(ContextManager):
|
||||
"""
|
||||
Context manager to trap errors reported by Leptonica < 1.79 or on Apple Silicon.
|
||||
|
||||
@@ -131,7 +132,7 @@ class _LeptonicaErrorTrap_Redirect:
|
||||
except Exception:
|
||||
self.leptonica_lock.release()
|
||||
raise
|
||||
return self
|
||||
return
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
# Restore old stderr
|
||||
@@ -171,7 +172,7 @@ tls = threading.local()
|
||||
tls.trap = None
|
||||
|
||||
|
||||
class _LeptonicaErrorTrap_Queue:
|
||||
class _LeptonicaErrorTrap_Queue(ContextManager):
|
||||
def __init__(self):
|
||||
self.queue = deque()
|
||||
|
||||
@@ -225,7 +226,7 @@ except (ffi.error, MemoryError):
|
||||
# Pre-1.79 Leptonica does not have leptSetStderrHandler
|
||||
# And some platforms, notably Apple ARM 64, do not allow the write+execute
|
||||
# memory needed to set up the callback function.
|
||||
_LeptonicaErrorTrap = _LeptonicaErrorTrap_Redirect
|
||||
_LeptonicaErrorTrap: Type[ContextManager] = _LeptonicaErrorTrap_Redirect
|
||||
else:
|
||||
# 1.79 have this new symbol
|
||||
_LeptonicaErrorTrap = _LeptonicaErrorTrap_Queue
|
||||
|
||||
Reference in New Issue
Block a user