Make ghostscript default backend and add support for string keywords

This commit is contained in:
Vinayak Mehta
2021-07-11 17:25:56 +05:30
parent f43235934b
commit 8abe02528b
8 changed files with 135 additions and 92 deletions
+2 -2
View File
@@ -29,8 +29,8 @@ class GhostscriptBackend(object):
def convert(self, pdf_path, png_path, resolution=300):
if not self.installed():
raise OSError(
"Ghostscript is not installed. Please install it using the instructions"
"here: https://camelot-py.readthedocs.io/en/master/user/install-deps.html"
"Ghostscript is not installed. You can install it using the instructions"
" here: https://camelot-py.readthedocs.io/en/master/user/install-deps.html"
)
import ghostscript
+5 -5
View File
@@ -3,21 +3,21 @@
from .poppler_backend import PopplerBackend
from .ghostscript_backend import GhostscriptBackend
backends = {"poppler": PopplerBackend, "ghostscript": GhostscriptBackend}
BACKENDS = {"poppler": PopplerBackend, "ghostscript": GhostscriptBackend}
class ImageConversionBackend(object):
def __init__(self, backend="poppler", use_fallback=True):
if backend not in backends.keys():
if backend not in BACKENDS.keys():
raise ValueError(f"Image conversion backend '{backend}' not supported")
self.backend = backend
self.use_fallback = use_fallback
self.fallbacks = list(filter(lambda x: x != backend, backends.keys()))
self.fallbacks = list(filter(lambda x: x != backend, BACKENDS.keys()))
def convert(self, pdf_path, png_path):
try:
converter = backends[self.backend]()
converter = BACKENDS[self.backend]()
converter.convert(pdf_path, png_path)
except Exception as e:
import sys
@@ -25,7 +25,7 @@ class ImageConversionBackend(object):
if self.use_fallback:
for fallback in self.fallbacks:
try:
converter = backends[fallback]()
converter = BACKENDS[fallback]()
converter.convert(pdf_path, png_path)
except Exception as e:
raise type(e)(
+1 -1
View File
@@ -9,7 +9,7 @@ class PopplerBackend(object):
pdftopng_executable = shutil.which("pdftopng")
if pdftopng_executable is None:
raise OSError(
"pdftopng is not installed. Please install it using the `pip install pdftopng` command."
"pdftopng is not installed. You can install it using the 'pip install pdftopng' command."
)
pdftopng_command = [pdftopng_executable, pdf_path, png_path]
+26 -3
View File
@@ -28,7 +28,7 @@ from ..image_processing import (
find_contours,
find_joints,
)
from ..backends import ImageConversionBackend
from ..backends.image_conversion import BACKENDS
logger = logging.getLogger("camelot")
@@ -111,7 +111,7 @@ class Lattice(BaseParser):
threshold_constant=-2,
iterations=0,
resolution=300,
backend=ImageConversionBackend(),
backend="ghostscript",
**kwargs,
):
self.table_regions = table_regions
@@ -129,7 +129,30 @@ class Lattice(BaseParser):
self.threshold_constant = threshold_constant
self.iterations = iterations
self.resolution = resolution
self.backend = backend
self.backend = Lattice._get_backend(backend)
@staticmethod
def _get_backend(backend):
def implements_convert():
methods = [
method for method in dir(backend) if method.startswith("__") is False
]
return "convert" in methods
if isinstance(backend, str):
if backend in BACKENDS.keys():
return BACKENDS[backend]()
else:
raise NotImplementedError(
f"Unknown backend '{backend}' specified. Please use either 'poppler' or 'ghostscript'."
)
else:
if not implements_convert():
raise NotImplementedError(
f"'{backend}' must implement a 'convert' method"
)
return backend
@staticmethod
def _reduce_index(t, idx, shift_text):