mirror of
https://github.com/PR0M3TH3AN/unredact.git
synced 2026-10-27 08:40:29 +00:00
feat: PDF rasterization module
Add rasterize_pdf() that converts PDF pages to PIL images using pdf2image. Supports configurable DPI and page range selection. Includes tests verifying multi-page output, high-DPI dimensions, and single-page extraction. Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
1 parent
94877952db
commit
420e20a8cf
2 files changed
+43
No files matched your search
@@ -0,0 +1,24 @@
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from unredact.pipeline.rasterize import rasterize_pdf
|
||||
|
||||
|
||||
def test_rasterize_returns_list_of_images(sample_pdf: Path):
|
||||
pages = rasterize_pdf(sample_pdf)
|
||||
assert len(pages) == 2 # EFTA00554620.pdf has 2 pages
|
||||
assert all(isinstance(p, Image.Image) for p in pages)
|
||||
|
||||
|
||||
def test_rasterize_high_dpi(sample_pdf: Path):
|
||||
pages = rasterize_pdf(sample_pdf, dpi=300)
|
||||
# At 300 DPI, a letter-size page is roughly 2550x3300
|
||||
w, h = pages[0].size
|
||||
assert w > 2000
|
||||
assert h > 3000
|
||||
|
||||
|
||||
def test_rasterize_single_page(sample_pdf: Path):
|
||||
pages = rasterize_pdf(sample_pdf, first_page=1, last_page=1)
|
||||
assert len(pages) == 1
|
||||
@@ -0,0 +1,19 @@
|
||||
from pathlib import Path
|
||||
|
||||
from pdf2image import convert_from_path
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def rasterize_pdf(
|
||||
pdf_path: Path,
|
||||
dpi: int = 300,
|
||||
first_page: int | None = None,
|
||||
last_page: int | None = None,
|
||||
) -> list[Image.Image]:
|
||||
"""Convert PDF pages to PIL images at the given DPI."""
|
||||
kwargs: dict = {"dpi": dpi}
|
||||
if first_page is not None:
|
||||
kwargs["first_page"] = first_page
|
||||
if last_page is not None:
|
||||
kwargs["last_page"] = last_page
|
||||
return convert_from_path(str(pdf_path), **kwargs)
|
||||
Reference in new issue
Block a user