Skip to content

Commit f5bd782

Browse files
committed
added pdf.to_imgs func
1 parent 984ed68 commit f5bd782

5 files changed

Lines changed: 41 additions & 3 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description = "Streamlines the process of preparing documents for LLM's."
88
readme = "README.md"
99
requires-python = ">=3.8"
1010
license = { text = "MIT" }
11-
version = "0.5.7"
11+
version = "0.5.8"
1212
authors = [{name = "Sergey Filimonov", email = "hello@sergey.fyi"}]
1313
dependencies = [
1414
"PyMuPDF >= 1.23.2",

src/openparse/pdf.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from pdfminer.high_level import extract_pages
1010
from pdfminer.layout import LTPage
11+
from PIL import Image
1112
from pydantic import BaseModel
1213
from pypdf import PdfReader, PdfWriter
1314

@@ -237,3 +238,23 @@ def _flip_coordinates(self, bbox: Bbox) -> Bbox:
237238
x1=bbox.x1,
238239
y1=fy1,
239240
)
241+
242+
def to_imgs(self, page_numbers: Optional[List[int]] = None) -> List[Image.Image]:
243+
doc = self.to_pymupdf_doc()
244+
images = []
245+
246+
if not doc.is_pdf:
247+
raise ValueError("The document is not in PDF format.")
248+
if doc.needs_pass:
249+
raise ValueError("The PDF document is password protected.")
250+
251+
if page_numbers is None:
252+
page_numbers = list(range(doc.page_count))
253+
254+
for n in page_numbers:
255+
page = doc[n]
256+
pix = page.get_pixmap()
257+
image = Image.frombytes("RGB", (pix.width, pix.height), pix.samples)
258+
images.append(image)
259+
260+
return images

src/openparse/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
OPEN_PARSE_VERSION = "0.5.7"
1+
OPEN_PARSE_VERSION = "0.5.8"
22

33

44
def version_info() -> str:

src/tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
6+
@pytest.hookimpl(tryfirst=True)
7+
def pytest_sessionstart(session):
8+
expected_dir = Path(__file__).resolve().parent / "src"
9+
10+
current_dir = Path.cwd()
11+
12+
if not current_dir == expected_dir:
13+
pytest.exit(
14+
"Pytest must be run from the project root directory",
15+
returncode=1,
16+
)

src/tests/test_doc_parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import openparse
21
import re
32

3+
import openparse
4+
45

56
def test_parse_doc():
67
basic_doc_path = "src/evals/data/full-pdfs/mock-1-page-lease.pdf"

0 commit comments

Comments
 (0)