Skip to content

Commit

Permalink
refactor: Updated version of pypdfium2 and its calls (#845)
Browse files Browse the repository at this point in the history
* Switch to new pypdfium2 API

* conf.py: fix intersphinx link

* apply review suggestions

* Fix the intersphinx function reference

I need to do something to make these paths shorter.
  • Loading branch information
mara004 authored Mar 15, 2022
1 parent bc03a0f commit 096eb48
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
6 changes: 6 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.intersphinx',
'sphinx.ext.viewcode',
'sphinx.ext.coverage',
'sphinx.ext.mathjax',
Expand All @@ -49,6 +50,11 @@
'sphinx_markdown_tables',
]

intersphinx_mapping = {
'python': ('https://docs.python.org/3', None),
'pypdfium2': ('https://pypdfium2.readthedocs.io/en/stable/', None),
}

napoleon_use_ivar = True

# Add any paths that contain templates here, relative to this directory.
Expand Down
14 changes: 9 additions & 5 deletions doctr/io/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This program is licensed under the Apache License version 2.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.

import os.path
from pathlib import Path
from typing import Any, List

Expand All @@ -24,15 +25,18 @@ def read_pdf(file: AbstractFile, scale: float = 2, **kwargs: Any) -> List[np.nda
Args:
file: the path to the PDF file
scale: rendering scale (1 corresponds to 72dpi)
kwargs: additional parameters to :func:`pypdfium2._helpers.pdf_renderer.render_pdf_topil`
Returns:
the list of pages decoded as numpy ndarray of shape H x W x 3
the list of pages decoded as numpy ndarray of shape H x W x C
"""

if not isinstance(file, (str, Path, bytes)):
if isinstance(file, Path):
file = str(file)
if not isinstance(file, (str, bytes)):
raise TypeError("unsupported object type for argument 'file'")

if isinstance(file, (str, Path)) and not Path(file).is_file():
if isinstance(file, str) and not os.path.isfile(file):
raise FileNotFoundError(f"unable to access {file}")

# Read pages with fitz and convert them to numpy ndarrays
return [np.asarray(img) for img, _ in pdfium.render_pdf(file, scale=scale)]
# Rasterise pages to PIL images with pypdfium2 and convert to numpy ndarrays
return [np.asarray(img) for img, _ in pdfium.render_pdf_topil(file, scale=scale, **kwargs)]
2 changes: 1 addition & 1 deletion requirements-pt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ numpy>=1.16.0
scipy>=1.4.0
h5py>=3.1.0
opencv-python>=3.4.5.20
pypdfium2>=0.14.0
pypdfium2>=1.0.0
pyclipper>=1.2.0
shapely>=1.6.0
matplotlib>=3.1.0,<3.4.3
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ numpy>=1.16.0
scipy>=1.4.0
h5py>=3.1.0
opencv-python>=3.4.5.20
pypdfium2>=0.14.0
pypdfium2>=1.0.0
pyclipper>=1.2.0
shapely>=1.6.0
matplotlib>=3.1.0,<3.4.3
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"h5py>=3.1.0",
"opencv-python>=3.4.5.20",
"tensorflow>=2.4.0",
"pypdfium2>=0.14.0",
"pypdfium2>=1.0.0",
"pyclipper>=1.2.0",
"shapely>=1.6.0",
"matplotlib>=3.1.0,<3.4.3",
Expand Down

0 comments on commit 096eb48

Please sign in to comment.