Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch PDF page rendering to an iterator format #1040

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions doctr/io/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.

from pathlib import Path
from typing import Any, List, Optional
from typing import Any, Iterator, Optional

import numpy as np
import pypdfium2 as pdfium
Expand All @@ -19,7 +19,7 @@ def read_pdf(
scale: float = 2,
password: Optional[str] = None,
**kwargs: Any,
) -> List[np.ndarray]:
) -> Iterator[np.ndarray]:
"""Read a PDF file and convert it into an image in numpy format

>>> from doctr.documents import read_pdf
Expand All @@ -42,4 +42,5 @@ def read_pdf(

# Rasterise pages to PIL images with pypdfium2 and convert to numpy ndarrays
with pdfium.PdfDocument(file, password=password) as pdf:
return [np.asarray(img) for img in pdf.render_topil(scale=scale, **kwargs)]
for img in pdf.render_topil(scale=scale, **kwargs):
yield np.asarray(img)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be a one-liner:

yield from pdf.render_topil(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mh looks a bit strange 😅
yield from np.asarray(pdf.render_topil(...))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I see! Overlooked that in the hurry. I expect it'll be better once I have released v3 so that you can use render_tonumpy(), see #1032.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A got it would you link the discussion to the issue ? :) So i would close this PR until your release is finally

2 changes: 1 addition & 1 deletion doctr/io/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def from_pdf(cls, file: AbstractFile, **kwargs) -> List[np.ndarray]:
the list of pages decoded as numpy ndarray of shape H x W x 3
"""

return read_pdf(file, **kwargs)
return list(read_pdf(file, **kwargs))

@classmethod
def from_url(cls, url: str, **kwargs) -> List[np.ndarray]:
Expand Down
8 changes: 4 additions & 4 deletions tests/common/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ def _check_doc_content(doc_tensors, num_pages):


def test_read_pdf(mock_pdf):
doc = io.read_pdf(mock_pdf)
doc = list(io.read_pdf(mock_pdf))
_check_doc_content(doc, 2)

with open(mock_pdf, "rb") as f:
doc = io.read_pdf(f.read())
doc = list(io.read_pdf(f.read()))
_check_doc_content(doc, 2)

# Wrong input type
with pytest.raises(TypeError):
_ = io.read_pdf(123)
_ = list(io.read_pdf(123))

# Wrong path
with pytest.raises(FileNotFoundError):
_ = io.read_pdf("my_imaginary_file.pdf")
_ = list(io.read_pdf("my_imaginary_file.pdf"))


def test_read_img_as_numpy(tmpdir_factory, mock_pdf):
Expand Down