python-doctr 1.0.1__py3-none-any.whl → 1.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. doctr/cli/__init__.py +0 -0
  2. doctr/cli/main.py +669 -0
  3. doctr/datasets/__init__.py +2 -0
  4. doctr/datasets/coco_text.py +4 -2
  5. doctr/datasets/cord.py +2 -1
  6. doctr/datasets/datasets/base.py +12 -24
  7. doctr/datasets/datasets/pytorch.py +14 -4
  8. doctr/datasets/detection.py +14 -3
  9. doctr/datasets/doc_artefacts.py +2 -1
  10. doctr/datasets/funsd.py +2 -1
  11. doctr/datasets/generator/base.py +121 -37
  12. doctr/datasets/generator/pytorch.py +5 -6
  13. doctr/datasets/ic03.py +9 -7
  14. doctr/datasets/ic13.py +4 -2
  15. doctr/datasets/iiit5k.py +2 -1
  16. doctr/datasets/iiithws.py +4 -2
  17. doctr/datasets/imgur5k.py +4 -2
  18. doctr/datasets/layout.py +129 -0
  19. doctr/datasets/mjsynth.py +4 -2
  20. doctr/datasets/ocr.py +2 -1
  21. doctr/datasets/orientation.py +2 -1
  22. doctr/datasets/recognition.py +5 -4
  23. doctr/datasets/sroie.py +2 -1
  24. doctr/datasets/svhn.py +2 -1
  25. doctr/datasets/svt.py +10 -8
  26. doctr/datasets/synthtext.py +2 -1
  27. doctr/datasets/table_structure.py +97 -0
  28. doctr/datasets/utils.py +104 -21
  29. doctr/datasets/vocabs.py +320 -148
  30. doctr/datasets/wildreceipt.py +4 -2
  31. doctr/file_utils.py +3 -1
  32. doctr/io/__init__.py +3 -0
  33. doctr/io/elements.py +381 -271
  34. doctr/io/exporters.py +1915 -0
  35. doctr/io/figures.py +313 -0
  36. doctr/io/image/base.py +54 -7
  37. doctr/io/image/pytorch.py +13 -5
  38. doctr/io/pdf.py +6 -5
  39. doctr/io/reader.py +268 -18
  40. doctr/io/sources.py +636 -0
  41. doctr/models/__init__.py +3 -0
  42. doctr/models/_utils.py +206 -13
  43. doctr/models/builder.py +590 -137
  44. doctr/models/classification/__init__.py +3 -0
  45. doctr/models/classification/hgnetv2/__init__.py +1 -0
  46. doctr/models/classification/hgnetv2/pytorch.py +352 -0
  47. doctr/models/classification/magc_resnet/pytorch.py +1 -1
  48. doctr/models/classification/mobilenet/pytorch.py +6 -6
  49. doctr/models/classification/predictor/pytorch.py +34 -2
  50. doctr/models/classification/resnet/pytorch.py +5 -5
  51. doctr/models/classification/starnet/__init__.py +1 -0
  52. doctr/models/classification/starnet/pytorch.py +202 -0
  53. doctr/models/classification/textnet/pytorch.py +6 -5
  54. doctr/models/classification/vgg/pytorch.py +3 -2
  55. doctr/models/classification/vip/layers/pytorch.py +4 -4
  56. doctr/models/classification/vip/pytorch.py +4 -4
  57. doctr/models/classification/vit/pytorch.py +3 -3
  58. doctr/models/classification/vit_det/__init__.py +1 -0
  59. doctr/models/classification/vit_det/layers/__init__.py +1 -0
  60. doctr/models/classification/vit_det/layers/pytorch.py +215 -0
  61. doctr/models/classification/vit_det/pytorch.py +346 -0
  62. doctr/models/classification/zoo.py +14 -2
  63. doctr/models/detection/_utils/base.py +23 -7
  64. doctr/models/detection/core.py +32 -18
  65. doctr/models/detection/differentiable_binarization/base.py +23 -9
  66. doctr/models/detection/differentiable_binarization/pytorch.py +10 -4
  67. doctr/models/detection/fast/base.py +22 -6
  68. doctr/models/detection/fast/pytorch.py +24 -11
  69. doctr/models/detection/linknet/base.py +22 -6
  70. doctr/models/detection/linknet/pytorch.py +10 -3
  71. doctr/models/detection/predictor/pytorch.py +43 -2
  72. doctr/models/detection/zoo.py +6 -2
  73. doctr/models/factory/hub.py +59 -41
  74. doctr/models/formula/__init__.py +2 -0
  75. doctr/models/formula/formulanet/__init__.py +2 -0
  76. doctr/models/formula/formulanet/base.py +70 -0
  77. doctr/models/formula/formulanet/pytorch.py +587 -0
  78. doctr/models/formula/predictor/__init__.py +1 -0
  79. doctr/models/formula/predictor/pytorch.py +132 -0
  80. doctr/models/formula/zoo.py +102 -0
  81. doctr/models/kie_predictor/base.py +23 -1
  82. doctr/models/kie_predictor/pytorch.py +87 -15
  83. doctr/models/layout/__init__.py +2 -0
  84. doctr/models/layout/lw_detr/__init__.py +1 -0
  85. doctr/models/layout/lw_detr/base.py +309 -0
  86. doctr/models/layout/lw_detr/layers/__init__.py +1 -0
  87. doctr/models/layout/lw_detr/layers/pytorch.py +785 -0
  88. doctr/models/layout/lw_detr/pytorch.py +895 -0
  89. doctr/models/layout/predictor/__init__.py +1 -0
  90. doctr/models/layout/predictor/pytorch.py +114 -0
  91. doctr/models/layout/zoo.py +94 -0
  92. doctr/models/modules/layers/pytorch.py +139 -3
  93. doctr/models/modules/transformer/pytorch.py +151 -31
  94. doctr/models/predictor/base.py +415 -17
  95. doctr/models/predictor/pytorch.py +109 -12
  96. doctr/models/preprocessor/pytorch.py +82 -25
  97. doctr/models/reading_order/__init__.py +1 -0
  98. doctr/models/reading_order/base.py +1127 -0
  99. doctr/models/recognition/core.py +14 -1
  100. doctr/models/recognition/crnn/pytorch.py +40 -16
  101. doctr/models/recognition/master/base.py +4 -1
  102. doctr/models/recognition/master/pytorch.py +84 -22
  103. doctr/models/recognition/parseq/base.py +4 -1
  104. doctr/models/recognition/parseq/pytorch.py +126 -25
  105. doctr/models/recognition/predictor/_utils.py +131 -14
  106. doctr/models/recognition/predictor/pytorch.py +68 -12
  107. doctr/models/recognition/sar/pytorch.py +21 -7
  108. doctr/models/recognition/utils.py +80 -50
  109. doctr/models/recognition/viptr/pytorch.py +38 -14
  110. doctr/models/recognition/vitstr/base.py +4 -1
  111. doctr/models/recognition/vitstr/pytorch.py +11 -7
  112. doctr/models/recognition/zoo.py +36 -9
  113. doctr/models/table_structure/__init__.py +2 -0
  114. doctr/models/table_structure/grid.py +430 -0
  115. doctr/models/table_structure/predictor/__init__.py +1 -0
  116. doctr/models/table_structure/predictor/pytorch.py +231 -0
  117. doctr/models/table_structure/tablecenternet/__init__.py +2 -0
  118. doctr/models/table_structure/tablecenternet/base.py +675 -0
  119. doctr/models/table_structure/tablecenternet/pytorch.py +525 -0
  120. doctr/models/table_structure/zoo.py +87 -0
  121. doctr/models/utils/pytorch.py +381 -25
  122. doctr/models/zoo.py +141 -4
  123. doctr/transforms/functional/base.py +6 -0
  124. doctr/transforms/functional/pytorch.py +134 -6
  125. doctr/transforms/modules/base.py +261 -82
  126. doctr/transforms/modules/pytorch.py +396 -76
  127. doctr/utils/common_types.py +21 -1
  128. doctr/utils/data.py +24 -11
  129. doctr/utils/fonts.py +64 -17
  130. doctr/utils/geometry.py +167 -28
  131. doctr/utils/metrics.py +481 -19
  132. doctr/utils/multithreading.py +127 -4
  133. doctr/utils/reconstitution.py +696 -132
  134. doctr/utils/visualization.py +73 -10
  135. doctr/version.py +1 -1
  136. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/METADATA +119 -249
  137. python_doctr-1.2.0.dist-info/RECORD +191 -0
  138. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/WHEEL +1 -1
  139. python_doctr-1.2.0.dist-info/entry_points.txt +2 -0
  140. python_doctr-1.0.1.dist-info/RECORD +0 -149
  141. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/licenses/LICENSE +0 -0
  142. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/top_level.txt +0 -0
  143. {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/zip-safe +0 -0
doctr/io/reader.py CHANGED
@@ -3,8 +3,10 @@
3
3
  # This program is licensed under the Apache License 2.0.
4
4
  # See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.
5
5
 
6
- from collections.abc import Sequence
6
+ import logging
7
+ from collections.abc import Callable, Sequence
7
8
  from pathlib import Path
9
+ from typing import Any, Literal, TypeAlias, overload
8
10
 
9
11
  import numpy as np
10
12
 
@@ -12,33 +14,117 @@ from doctr.file_utils import requires_package
12
14
  from doctr.utils.common_types import AbstractFile
13
15
 
14
16
  from .html import read_html
15
- from .image import read_img_as_numpy
16
- from .pdf import read_pdf
17
+ from .sources import (
18
+ _PAGE_ERRORS,
19
+ IMAGE_EXTENSIONS,
20
+ ConcatSource,
21
+ ImageSource,
22
+ PageDecodeError,
23
+ PageSource,
24
+ PDFSource,
25
+ _file_name,
26
+ _is_pdf,
27
+ _list_files,
28
+ _UnreadablePage,
29
+ )
17
30
 
18
31
  __all__ = ["DocumentFile"]
19
32
 
33
+ logger = logging.getLogger(__name__)
34
+
35
+ # What to do with a file that cannot be read (missing, not an image, a corrupted PDF...): raise the error, skip it
36
+ # with a warning, or skip it and call a function with the file and the error
37
+ OnFileError: TypeAlias = Literal["raise", "skip"] | Callable[[AbstractFile, Exception], Any]
38
+
39
+
40
+ def _skip_file(on_error: OnFileError, file: AbstractFile, error: Exception) -> None:
41
+ """Report a file that cannot be read, and is left out of the document"""
42
+ if callable(on_error):
43
+ on_error(file, error)
44
+ else:
45
+ logger.warning(f"Skipping {_file_name(file)}, which cannot be read: {error}")
46
+
47
+
48
+ def _file_at(doc: ConcatSource, origins: list[AbstractFile | None], idx: int) -> AbstractFile:
49
+ """The input of a page of a concatenation built by `DocumentFile.from_files`
50
+
51
+ Args:
52
+ doc: the concatenation
53
+ origins: the input of each source of `doc` (a path, bytes or a URL), or None for a source of images
54
+ idx: the page index in the concatenation
55
+
56
+ Returns:
57
+ the input the page comes from, e.g. the URL of a rendered web page rather than its PDF content
58
+ """
59
+ source_idx, page_idx = doc.locate(idx)
60
+ source = doc.sources[source_idx]
61
+ return source.files[page_idx] if isinstance(source, ImageSource) else origins[source_idx] # type: ignore[return-value]
62
+
63
+
64
+ def _materialize(source: PageSource) -> list[np.ndarray]:
65
+ """Decode every page of a source into memory"""
66
+ try:
67
+ return list(source)
68
+ finally:
69
+ source.close()
70
+
20
71
 
21
72
  class DocumentFile:
22
- """Read a document from multiple extensions"""
73
+ """Read a document from multiple extensions
74
+
75
+ Every reader decodes all the pages into a list of numpy arrays by default. Pass `lazy=True` to get a
76
+ :class:`~doctr.io.PageSource` instead: it behaves like that list, but a page is only decoded when it is
77
+ accessed. Predictors process page sources chunk by chunk, so that very long documents (1000+ pages) or
78
+ huge collections of images can be passed in a single call with a bounded memory footprint.
79
+ """
80
+
81
+ @overload
82
+ @classmethod
83
+ def from_pdf(cls, file: AbstractFile, lazy: Literal[False] = ..., **kwargs: Any) -> list[np.ndarray]: ...
23
84
 
85
+ @overload
24
86
  @classmethod
25
- def from_pdf(cls, file: AbstractFile, **kwargs) -> list[np.ndarray]:
87
+ def from_pdf(cls, file: AbstractFile, lazy: Literal[True], **kwargs: Any) -> PDFSource: ...
88
+
89
+ @overload
90
+ @classmethod
91
+ def from_pdf(cls, file: AbstractFile, lazy: bool, **kwargs: Any) -> list[np.ndarray] | PDFSource: ...
92
+
93
+ @classmethod
94
+ def from_pdf(cls, file: AbstractFile, lazy: bool = False, **kwargs: Any) -> list[np.ndarray] | PDFSource:
26
95
  """Read a PDF file
27
96
 
28
97
  >>> from doctr.io import DocumentFile
29
98
  >>> doc = DocumentFile.from_pdf("path/to/your/doc.pdf")
99
+ >>> # Pages are rendered on demand: suited for very long documents
100
+ >>> doc = DocumentFile.from_pdf("path/to/your/long_doc.pdf", lazy=True)
30
101
 
31
102
  Args:
32
103
  file: the path to the PDF file or a binary stream
33
- **kwargs: additional parameters to :meth:`pypdfium2.PdfPage.render`
104
+ lazy: if True, return a :class:`~doctr.io.PDFSource` rendering the pages on demand
105
+ **kwargs: additional parameters to :class:`~doctr.io.PDFSource` (`scale`, `rgb_mode`, `password`)
106
+ and :meth:`pypdfium2.PdfPage.render`
34
107
 
35
108
  Returns:
36
- the list of pages decoded as numpy ndarray of shape H x W x 3
109
+ the list of pages decoded as numpy ndarray of shape H x W x 3, or a lazy source if `lazy` is True
37
110
  """
38
- return read_pdf(file, **kwargs)
111
+ source = PDFSource(file, **kwargs)
112
+ return source if lazy else _materialize(source)
39
113
 
114
+ @overload
40
115
  @classmethod
41
- def from_url(cls, url: str, **kwargs) -> list[np.ndarray]:
116
+ def from_url(cls, url: str, lazy: Literal[False] = ..., **kwargs: Any) -> list[np.ndarray]: ...
117
+
118
+ @overload
119
+ @classmethod
120
+ def from_url(cls, url: str, lazy: Literal[True], **kwargs: Any) -> PDFSource: ...
121
+
122
+ @overload
123
+ @classmethod
124
+ def from_url(cls, url: str, lazy: bool, **kwargs: Any) -> list[np.ndarray] | PDFSource: ...
125
+
126
+ @classmethod
127
+ def from_url(cls, url: str, lazy: bool = False, **kwargs: Any) -> list[np.ndarray] | PDFSource:
42
128
  """Interpret a web page as a PDF document
43
129
 
44
130
  >>> from doctr.io import DocumentFile
@@ -46,10 +132,11 @@ class DocumentFile:
46
132
 
47
133
  Args:
48
134
  url: the URL of the target web page
49
- **kwargs: additional parameters to :meth:`pypdfium2.PdfPage.render`
135
+ lazy: if True, return a :class:`~doctr.io.PDFSource` rendering the pages on demand
136
+ **kwargs: additional parameters to :class:`~doctr.io.PDFSource` and :meth:`pypdfium2.PdfPage.render`
50
137
 
51
138
  Returns:
52
- the list of pages decoded as numpy ndarray of shape H x W x 3
139
+ the list of pages decoded as numpy ndarray of shape H x W x 3, or a lazy source if `lazy` is True
53
140
  """
54
141
  requires_package(
55
142
  "weasyprint",
@@ -57,23 +144,186 @@ class DocumentFile:
57
144
  + "Installation instructions: https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation",
58
145
  )
59
146
  pdf_stream = read_html(url)
60
- return cls.from_pdf(pdf_stream, **kwargs)
147
+ source = PDFSource(pdf_stream, **kwargs)
148
+ return source if lazy else _materialize(source)
61
149
 
150
+ @overload
62
151
  @classmethod
63
- def from_images(cls, files: Sequence[AbstractFile] | AbstractFile, **kwargs) -> list[np.ndarray]:
152
+ def from_images(
153
+ cls, files: Sequence[AbstractFile] | AbstractFile, lazy: Literal[False] = ..., **kwargs: Any
154
+ ) -> list[np.ndarray]: ...
155
+
156
+ @overload
157
+ @classmethod
158
+ def from_images(
159
+ cls, files: Sequence[AbstractFile] | AbstractFile, lazy: Literal[True], **kwargs: Any
160
+ ) -> ImageSource: ...
161
+
162
+ @overload
163
+ @classmethod
164
+ def from_images(
165
+ cls, files: Sequence[AbstractFile] | AbstractFile, lazy: bool, **kwargs: Any
166
+ ) -> list[np.ndarray] | ImageSource: ...
167
+
168
+ @classmethod
169
+ def from_images(
170
+ cls, files: Sequence[AbstractFile] | AbstractFile, lazy: bool = False, **kwargs: Any
171
+ ) -> list[np.ndarray] | ImageSource:
64
172
  """Read an image file (or a collection of image files) and convert it into an image in numpy format
65
173
 
66
174
  >>> from doctr.io import DocumentFile
67
175
  >>> pages = DocumentFile.from_images(["path/to/your/page1.png", "path/to/your/page2.png"])
176
+ >>> # Every image of a folder (sorted by name), decoded on demand
177
+ >>> pages = DocumentFile.from_images("path/to/your/folder", lazy=True)
68
178
 
69
179
  Args:
70
- files: the path to the image file or a binary stream, or a collection of those
180
+ files: the path to an image file, to a directory of images, or an image as bytes, or a collection of those
181
+ lazy: if True, return a :class:`~doctr.io.ImageSource` decoding the images on demand
71
182
  **kwargs: additional parameters to :meth:`doctr.io.image.read_img_as_numpy`
72
183
 
73
184
  Returns:
74
- the list of pages decoded as numpy ndarray of shape H x W x 3
185
+ the list of pages decoded as numpy ndarray of shape H x W x 3, or a lazy source if `lazy` is True
75
186
  """
76
- if isinstance(files, (str, Path, bytes)):
77
- files = [files]
187
+ source = ImageSource(files, **kwargs)
188
+ return source if lazy else _materialize(source)
189
+
190
+ @overload
191
+ @classmethod
192
+ def from_files(
193
+ cls,
194
+ files: Sequence[AbstractFile] | AbstractFile,
195
+ lazy: Literal[False] = ...,
196
+ pdf_kwargs: dict[str, Any] | None = ...,
197
+ image_kwargs: dict[str, Any] | None = ...,
198
+ on_error: OnFileError = ...,
199
+ ) -> list[np.ndarray]: ...
200
+
201
+ @overload
202
+ @classmethod
203
+ def from_files(
204
+ cls,
205
+ files: Sequence[AbstractFile] | AbstractFile,
206
+ lazy: Literal[True],
207
+ pdf_kwargs: dict[str, Any] | None = ...,
208
+ image_kwargs: dict[str, Any] | None = ...,
209
+ on_error: OnFileError = ...,
210
+ ) -> ConcatSource: ...
211
+
212
+ @overload
213
+ @classmethod
214
+ def from_files(
215
+ cls,
216
+ files: Sequence[AbstractFile] | AbstractFile,
217
+ lazy: bool,
218
+ pdf_kwargs: dict[str, Any] | None = ...,
219
+ image_kwargs: dict[str, Any] | None = ...,
220
+ on_error: OnFileError = ...,
221
+ ) -> list[np.ndarray] | ConcatSource: ...
222
+
223
+ @classmethod
224
+ def from_files(
225
+ cls,
226
+ files: Sequence[AbstractFile] | AbstractFile,
227
+ lazy: bool = False,
228
+ pdf_kwargs: dict[str, Any] | None = None,
229
+ image_kwargs: dict[str, Any] | None = None,
230
+ on_error: OnFileError = "raise",
231
+ ) -> list[np.ndarray] | ConcatSource:
232
+ """Read any mix of PDFs, images, directories and URLs as a single document, in the given order
233
+
234
+ >>> from doctr.io import DocumentFile
235
+ >>> doc = DocumentFile.from_files(["cover.png", "report.pdf", "path/to/scans/"], lazy=True)
236
+ >>> source_idx, page_idx = doc.locate(42) # the source (in `doc.sources`) of the 43rd page
237
+
238
+ Args:
239
+ files: a path (to a PDF, an image or a directory), a URL, a PDF or an image as bytes,
240
+ or a collection of those. Directories are expanded (non-recursively) to the PDFs and images they
241
+ contain, sorted by name.
242
+ lazy: if True, return a :class:`~doctr.io.ConcatSource` decoding the pages on demand
243
+ pdf_kwargs: keyword arguments of :class:`~doctr.io.PDFSource`, applied to every PDF and URL
244
+ image_kwargs: keyword arguments of :meth:`doctr.io.image.read_img_as_numpy`, applied to every image
245
+ on_error: what to do with a file that cannot be read (missing, not an image, a corrupted PDF, an
246
+ unreachable URL...): "raise" (default) the error, "skip" the file with a warning, or a callable to
247
+ skip it and call with the file (or URL) and the error. Images are checked from their header: a
248
+ corrupted content is only detected when the image is decoded, i.e. here if `lazy` is False, and by
249
+ the predictor otherwise (cf. the `on_error` argument of
250
+ :meth:`~doctr.models.predictor.OCRPredictor.stream`).
251
+
252
+ Returns:
253
+ the list of pages decoded as numpy ndarray of shape H x W x 3, or a lazy source if `lazy` is True
254
+ """
255
+ if not (on_error in ("raise", "skip") or callable(on_error)):
256
+ raise ValueError(f"`on_error` must be 'raise', 'skip' or a callable, got {on_error!r}")
257
+ pdf_kwargs = pdf_kwargs or {}
258
+ image_kwargs = image_kwargs or {}
259
+
260
+ def _readable(file: AbstractFile, open_file: Callable[[], Any]) -> Any:
261
+ # The opened file, or None if it cannot be read and errors are skipped
262
+ try:
263
+ return open_file()
264
+ except _PAGE_ERRORS as e:
265
+ if on_error == "raise":
266
+ raise
267
+ _skip_file(on_error, file, e)
268
+ return None
269
+
270
+ inputs: list[AbstractFile] = []
271
+ for file in [files] if isinstance(files, (str, Path, bytes)) else files:
272
+ if isinstance(file, (str, Path)) and Path(file).is_dir():
273
+ inputs.extend(_list_files(Path(file), (".pdf", *IMAGE_EXTENSIONS)))
274
+ else:
275
+ inputs.append(file)
276
+
277
+ def _open_url(url: str) -> PDFSource:
278
+ try:
279
+ return cls.from_url(url, lazy=True, **pdf_kwargs)
280
+ except (PageDecodeError, OSError) as e:
281
+ # An unreachable page (`weasyprint.urls.URLFetchingError` is an `OSError`), or one rendered to an
282
+ # invalid PDF: a file that cannot be read, named by its URL rather than by its PDF content
283
+ raise PageDecodeError(f"unable to read the web page {url}: {e}") from e
284
+
285
+ sources: list[PageSource] = []
286
+ # The input of each source (None for a source of images, which knows its files), to name the faulty input of
287
+ # a page that cannot be decoded
288
+ origins: list[AbstractFile | None] = []
289
+ images: list[AbstractFile] = []
290
+
291
+ def _add_source(source: PageSource | None, origin: AbstractFile | None) -> None:
292
+ if source is not None:
293
+ sources.append(source)
294
+ origins.append(origin)
295
+
296
+ def _flush_images() -> None:
297
+ # Consecutive images are grouped into a single source
298
+ if images:
299
+ _add_source(ImageSource(list(images), **image_kwargs), None)
300
+ images.clear()
301
+
302
+ for file in inputs:
303
+ if isinstance(file, str) and file.lower().startswith(("http://", "https://")):
304
+ _flush_images()
305
+ _add_source(_readable(file, lambda: _open_url(file)), file)
306
+ elif isinstance(file, (str, Path, bytes)) and _is_pdf(file):
307
+ _flush_images()
308
+ _add_source(_readable(file, lambda: PDFSource(file, **pdf_kwargs)), file)
309
+ elif on_error == "raise" or _readable(file, lambda: ImageSource([file], **image_kwargs)) is not None:
310
+ # When skipping errors, every image is checked on its own, so that one bad file only skips itself
311
+ images.append(file)
312
+ _flush_images()
78
313
 
79
- return [read_img_as_numpy(file, **kwargs) for file in files]
314
+ source = ConcatSource(sources)
315
+ if lazy:
316
+ return source
317
+ if on_error == "raise":
318
+ return _materialize(source)
319
+ # Decode every page, skipping those that cannot be read
320
+ pages: list[np.ndarray] = []
321
+ try:
322
+ for idx, page in enumerate(source._iter_indices(range(len(source)), skip_errors=True)):
323
+ if isinstance(page, _UnreadablePage):
324
+ _skip_file(on_error, _file_at(source, origins, idx), page.error)
325
+ else:
326
+ pages.append(page)
327
+ finally:
328
+ source.close()
329
+ return pages