Reference
paves.image
Various ways of converting PDFs to images for feeding them to models and/or visualisation.`
BoxFunc = Callable[[Boxable], Rect]
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.
NotInstalledError
Bases: RuntimeError
Exception raised if the dependencies for a particular PDF to image backend are not installed.
Source code in src/paves/image.py
39 40 41 | |
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.py
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 | |
color_maker(spec, default='red')
Create a function that makes colors.
Source code in src/paves/image.py
523 524 525 526 | |
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.py
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 | |
get_box(obj)
Default function to get the bounding box for an object.
Source code in src/paves/image.py
388 389 390 391 392 393 | |
get_box_annotation(obj)
Get the bounding box of an Annotation
Source code in src/paves/image.py
409 410 411 412 | |
get_box_content(obj)
Get the bounding box of a ContentObject
Source code in src/paves/image.py
402 403 404 405 406 | |
get_box_rect(obj)
Get the bounding box of a ContentObject
Source code in src/paves/image.py
396 397 398 399 | |
get_label(obj)
Default function to get the label text for an object.
Source code in src/paves/image.py
415 416 417 418 | |
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.py
427 428 429 430 431 432 433 434 435 | |
get_label_content(obj)
Get the label text for a ContentObject.
Source code in src/paves/image.py
421 422 423 424 | |
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.py
438 439 440 441 442 443 444 445 446 | |
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.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 | |
pdfium(pdf, *, dpi=0, width=0, height=0)
Convert a PDF to images using PyPDFium2
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. Page width and height are
available in the info property of the images.
Raises:
ValueError: Invalid arguments (e.g. both dpi and width/height)
NotInstalledError: If PyPDFium2 is not installed.
Source code in src/paves/image.py
273 274 275 276 277 278 279 280 281 282 283 284 285 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 | |
pillow_color(color)
Convert colors to a form acceptable to Pillow.
Source code in src/paves/image.py
511 512 513 514 515 516 517 518 519 520 | |
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.py
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 199 200 201 202 203 204 205 206 207 208 209 210 | |
show(page, dpi=72)
Show a single page with some reasonable defaults.
Source code in src/paves/image.py
367 368 369 | |
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.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.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.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.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.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.
TableObject
dataclass
Bases: ContentObject
Table on one page of a PDF.
This is a ContentObject and can be treated as one (notably
with paves.image functions).
It could either come from a logical structure element, or it could
simply be a bounding box (as detected by some sort of visual
model). While these TableObjects will never span multiple
pages, the underlying logical structure element may do so. This
is currently the only way to detect multi-page tables through this
interface (they will have an equivalent parent property).
Note that the graphics state and coordinate transformation matrix may just be the page defaults, if Machine Learning™ was used to detect the table in a rendered image of the page.
Source code in src/paves/tables.py
27 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 71 72 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
table_bounds_to_objects(pdf, bounds)
Create TableObjects from detected bounding boxes.
Source code in src/paves/tables.py
249 250 251 252 253 254 255 256 257 | |
table_elements(pdf)
Iterate over all text objects in a PDF, page, or pages
Source code in src/paves/tables.py
141 142 143 144 145 146 | |
table_elements_to_objects(elements, page=None)
Make TableObjects from Elements.
Source code in src/paves/tables.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
tables(pdf, **kwargs)
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.
For the moment, this only works on tagged and accessible PDFs.
So, like paves.image, it can also use Machine Learning Models™
to do so, 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.py
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 | |
tables_detr(pdf, device='cpu')
Identify tables in a PDF or one of its pages using IBM's RT-DETR layout detection model
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
Union[str, PathLike, Document, Page, PageList]
|
PLAYA-PDF document, page, pages, or path to a PDF. |
required |
device
|
str
|
Torch device for running the model. |
'cpu'
|
Returns:
| Type | Description |
|---|---|
Union[Iterator[TableObject], None]
|
An iterator over |
Source code in src/paves/tables.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
tables_orelse(pdf, **kwargs)
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.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
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.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |