Reference
paves.image
Various ways of converting PDFs to images for feeding them to models and/or visualisation.`
BoxFunc = Callable[[Boxable], Union[Rect, None]]
module-attribute
Function to get a bounding box for a Boxable.
Boxable = Union[Annotation, ContentObject, Element, HasBbox, Rect]
module-attribute
Object for which we can get a bounding box.
Color = Union[str, Tuple[int, int, int], Tuple[float, float, float]]
module-attribute
Type alias for things that can be used as colors.
ColorMaker = Callable[[str], PillowColor]
module-attribute
Function that makes a Pillow color for a string label.
Colors = Union[Color, List[Color], Dict[str, Color]]
module-attribute
Type alias for colors or collections of colors.
DEFAULT_COLOR_CYCLE = ['blue', 'orange', 'green', 'red', 'purple', 'brown', 'pink', 'gray', 'olive', 'cyan']
module-attribute
Default color cycle (same as matplotlib)
LabelFunc = Callable[[Boxable], Any]
module-attribute
Function to get a label for a Boxable.
PillowColor = Union[str, Tuple[int, int, int]]
module-attribute
Type alias for things Pillow accepts as colors.
box(objs, *, color=DEFAULT_COLOR_CYCLE, label=True, label_color='white', label_size=9, label_margin=1, label_fill=True, image=None, labelfunc=get_label, boxfunc=get_box, dpi=72, page=None)
Draw boxes around things in a page of a PDF.
Source code in src/paves/image/__init__.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
color_maker(spec, default='red')
Create a function that makes colors.
Source code in src/paves/image/__init__.py
181 182 183 184 | |
convert(pdf, *, dpi=0, width=0, height=0)
Convert a PDF to images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
Union[str, PathLike, Document, Page, PageList]
|
PLAYA-PDF document, page, pages, or path to a PDF. |
required |
dpi
|
int
|
Render to this resolution (default is 72 dpi). |
0
|
width
|
int
|
Render to this width in pixels (0 to keep aspect ratio). |
0
|
height
|
int
|
Render to this height in pixels (0 to keep aspect ratio). |
0
|
Yields:
Pillow Image.Image objects, one per page. The original page
width and height in default user space units are available in
the info property of these images as page_width and
page_height
Raises:
ValueError: Invalid arguments (e.g. both dpi and width/height)
NotInstalledError: If no renderer is available
Source code in src/paves/image/converters.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
get_box(obj)
Default function to get the bounding box for an object.
Source code in src/paves/image/__init__.py
59 60 61 62 63 64 | |
get_box_rect(obj)
Get the bounding box of a bounding box
Source code in src/paves/image/__init__.py
67 68 69 70 | |
get_label(obj)
Default function to get the label text for an object.
Source code in src/paves/image/__init__.py
73 74 75 76 | |
get_label_annotation(obj)
Get the default label text for an Annotation.
This is just a default.
This is one of many possible options, so you may wish to define your own custom LabelFunc.
Source code in src/paves/image/__init__.py
85 86 87 88 89 90 91 92 93 | |
get_label_content(obj)
Get the label text for a ContentObject.
Source code in src/paves/image/__init__.py
79 80 81 82 | |
get_label_element(obj)
Get the default label text for an Element.
This is just a default.
This is one of many possible options, so you may wish to define your own custom LabelFunc.
Source code in src/paves/image/__init__.py
96 97 98 99 100 101 102 103 104 | |
mark(objs, *, color=DEFAULT_COLOR_CYCLE, transparency=0.75, label=False, label_color='white', label_size=9, label_margin=1, outline=False, image=None, labelfunc=get_label, boxfunc=get_box, dpi=72, page=None)
Highlight things in a page of a PDF.
Source code in src/paves/image/__init__.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
pillow_color(color)
Convert colors to a form acceptable to Pillow.
Source code in src/paves/image/__init__.py
169 170 171 172 173 174 175 176 177 178 | |
popple(pdf, *, dpi=0, width=0, height=0)
Convert a PDF to images using Poppler's pdftoppm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
Union[str, PathLike, Document, Page, PageList]
|
PLAYA-PDF document, page, pages, or path to a PDF. |
required |
dpi
|
int
|
Render to this resolution (default is 72 dpi). |
0
|
width
|
int
|
Render to this width in pixels. |
0
|
height
|
int
|
Render to this height in pixels. |
0
|
Yields:
Pillow Image.Image objects, one per page.
Raises:
ValueError: Invalid arguments (e.g. both dpi and width/height)
NotInstalledError: If Poppler is not installed.
Source code in src/paves/image/poppler.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
show(page, dpi=72)
Show a single page with some reasonable defaults.
Source code in src/paves/image/__init__.py
32 33 34 35 36 37 38 39 40 | |
paves.text
Various somewhat-more-heuristic ways of guessing, getting, and processing text in PDFs.
WordObject
dataclass
Bases: TextBase
"Word" in a PDF.
This is heuristically determined, either by explicit whitespace (if you're lucky enough to have a Tagged PDF) or by a sufficient gap between adjacent glyphs (otherwise).
It otherwise behaves just like a TextObject. You can iterate
over its glyphs, etc. But, as a treat, these glyphs are
"finalized" so you don't have to worry about inconsistent graphics
states and so forth, and you also get some convenience properties.
The origin of the curent (logical) line is also available, to
facilitate grouping words into lines, if you so desire (simply
use itertools.groupby(words, paves.text.line))
Source code in src/paves/text/__init__.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
line_break(glyph, predicted_origin)
Heuristically predict a line break based on the predicted origin from the previous glyph.
Source code in src/paves/text/__init__.py
95 96 97 98 99 100 101 102 103 104 105 106 | |
text_objects(pdf)
Iterate over all text objects in a PDF, page, or pages
Source code in src/paves/text/__init__.py
109 110 111 112 113 114 | |
word_break(glyph, predicted_origin, prev_displacement)
Heuristically predict a word break based on the predicted origin from the previous glyph.
Source code in src/paves/text/__init__.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
words(pdf)
Extract "words" (i.e. whitespace-separated text cells) from a PDF or one of its pages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
Union[str, PathLike, Document, Page, PageList]
|
PLAYA-PDF document, page, pages, or path to a PDF. |
required |
Yields:
| Type | Description |
|---|---|
WordObject
|
|
WordObject
|
functions, or you can do various other things with them too. |
Source code in src/paves/text/__init__.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
paves.tables
Simple and not at all Java-damaged interface for table detection.
detector(name)
Look up a detector by name.
Source code in src/paves/tables/detectors.py
39 40 41 42 43 44 | |
tables(pdf)
Identify tables in a PDF or one of its pages.
This will always try to use logical structure (via PLAYA-PDF) first to identify tables.
Of course, that only works on tagged and accessible PDFs. So,
like paves.image, we can also use Machine Learning Models™ here,
which involves nasty horrible dependencyses (we hates them, they
stole the precious) like cudnn-10-gigabytes-of-c++.
If you'd like to try that, then you can do so by installing the
transformers[torch] package (if you don't have a GPU, try adding
--extra-index-url https://download.pytorch.org/whl/cpu to pip's
command line).
These tables cannot span multiple pages.
Often, a table will span multiple pages. With PDF logical
structure, this can be represented (and sometimes is), but if
there is no logical structure, this is not possible, since
tables are detected from the rendered image of a page.
Reconstructing this information is both extremely important
and also very difficult with current models (perhaps very big
VLMs can do it?). Since we also want to visualize tables with
paves.image, we don't return multi-page tables here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
Union[str, PathLike, Document, Page, PageList]
|
PLAYA-PDF document, page, pages, or path to a PDF. |
required |
Returns:
| Type | Description |
|---|---|
Iterator[TableObject]
|
An iterator over |
Iterator[TableObject]
|
detect tables, this will return an iterator over an empty |
Iterator[TableObject]
|
list. You may wish to use |
Iterator[TableObject]
|
tables can be detected. |
Source code in src/paves/tables/detectors.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
tables_detr(pdf)
Identify tables in a PDF or one of its pages using Docling Project layout model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
Union[str, PathLike, Document, Page, PageList]
|
PLAYA-PDF document, page, pages, or path to a PDF. |
required |
Returns:
| Type | Description |
|---|---|
Union[Iterator[TableObject], None]
|
An iterator over |
Source code in src/paves/tables/detr.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
tables_orelse(pdf)
Identify tables in a PDF or one of its pages, or fail.
This works like tables but forces you (if you use type checking)
to detect the case where tables cannot be detected by any known
method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
Union[str, PathLike, Document, Page, PageList]
|
PLAYA-PDF document, page, pages, or path to a PDF. |
required |
Returns:
| Type | Description |
|---|---|
Union[Iterator[TableObject], None]
|
An iterator over |
Union[Iterator[TableObject], None]
|
method available to detect tables. This will cause a |
Union[Iterator[TableObject], None]
|
|
Source code in src/paves/tables/detectors.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
tables_structure(pdf)
Identify tables in a PDF or one of its pages using logical structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
Union[str, PathLike, Document, Page, PageList]
|
PLAYA-PDF document, page, pages, or path to a PDF. |
required |
Returns:
| Type | Description |
|---|---|
Union[Iterator[TableObject], None]
|
An iterator over |
Union[Iterator[TableObject], None]
|
logical structure (this will cause a TypeError, if you don't |
Union[Iterator[TableObject], None]
|
check for it). |
Source code in src/paves/tables/structure.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
tables_tatr(pdf)
Identify tables in a PDF or one of its pages using Microsoft Table Transformer model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
Union[str, PathLike, Document, Page, PageList]
|
PLAYA-PDF document, page, pages, or path to a PDF. |
required |
Returns:
| Type | Description |
|---|---|
Union[Iterator[TableObject], None]
|
An iterator over |
Source code in src/paves/tables/detr.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |