PDF to image in Python for OCR: 3 libraries tested
8
of 297 words at pypdfium2's default 72 dpi
Use pypdfium2, and always pass the dpi
| When | Do this |
|---|---|
| Any PDF | pypdfium2: about as many words, the least memory, no Poppler |
| Every call | Pass the dpi. pdf2image defaults to 200, PyMuPDF and pypdfium2 to 72 |
| Small print | Render at 400 dpi |
| Scanned PDF, one image a page | Take out the page image. No render at all |
The default dpi is the trap
| Render | Scan, of 1,955 | 7 pt page, of 297 |
|---|---|---|
| pdf2image, no dpi (200) | 1,877 | 228 |
| PyMuPDF, no dpi (72) | 1,878 | 7 |
| pypdfium2, no dpi (72) | 1,889 | 8 |
| All 3 at 300 dpi, color | 1,874 to 1,876 | 215 to 219 |
| All 3 at 400 dpi, color | 1,871 to 1,875 | 284 to 290 |
The scan's big book type read about the same at every dpi, 72 to 400, so big type hides the trap. Small print does not.
Why did 300 dpi score lower than 200?
The 7 pt page is a table. At 150 to 300 dpi, Tesseract read it column by column: all the statements, then the numbers. At 400 dpi it read it row by row. The score counts words in order, so a column read loses most of the numbers.
| dpi | In order | Any order |
|---|---|---|
| 200 | 228 | 261 to 279 |
| 300 | 215 to 219 | 253 to 264 |
| 400 | 284 to 290 | 286 to 290 |
A small pixel change can flip it: pdf2image with
use_pdftocairo=True found 273 in order at 300 dpi. One
page, so read the 7 pt column as a rough guide.
pypdfium2 used the least memory
| Render | Seconds | Peak MB |
|---|---|---|
| pdf2image | 2.00 | 733 |
| pdf2image, gray | 1.55 | 276 |
| PyMuPDF | 0.87 | 690 |
| PyMuPDF, gray | 2.13 | 501 |
| pypdfium2 | 0.58 | 327 |
| pypdfium2, gray | 0.56 | 172 |
Seconds are rough: other jobs ran on the Mac, so trust only big gaps. Peak MB includes about 50 to 70 MB of Python start. For pdf2image it leaves out pdftoppm, which used about 20 to 90 MB more.
The code: 300 dpi, gray
pypdfium2, with pip install pypdfium2:
import pypdfium2 as pdfium
pdf = pdfium.PdfDocument("scan.pdf")
pages = [p.render(scale=300 / 72, grayscale=True).to_pil() for p in pdf]
print(len(pages), pages[0].size, pages[0].mode)
PyMuPDF (the import name is pymupdf):
import pymupdf
with pymupdf.open("scan.pdf") as doc:
pages = [p.get_pixmap(dpi=300, colorspace="gray").pil_image() for p in doc]
print(len(pages), pages[0].size, pages[0].mode)
With PyMuPDF, gray was slower at every dpi (2.13 s against 0.87 s at
300). Drop colorspace="gray" if time matters more than
memory.
pdf2image, after brew install poppler:
from pdf2image import convert_from_path
pages = convert_from_path("scan.pdf", dpi=300, grayscale=True)
print(len(pages), pages[0].size, pages[0].mode)
For small print, change 300 to 400. Each gives a list of Pillow images, ready for OCR: pytesseract PDF to text.
Typed Arabic PDF? Try its own text before OCR: extract Arabic text from PDF.
Scanned PDF? Take the image out
Each scan page is already a 300 ppi JPEG. Take it out as it is:
1,877 words; 300 dpi renders got 1,874 to 1,880
6.5 MB of JPEGs, not 37 MB of PNGs
import pymupdf
with pymupdf.open("scan.pdf") as doc:
for i, page in enumerate(doc, 1):
for xref, *_ in page.get_images(full=True):
img = doc.extract_image(xref)
with open(f"page-{i}.{img['ext']}", "wb") as f:
f.write(img["image"])
print(i, "pages,", img["ext"], img["width"], img["height"])
No Python: pdfimages -all scan.pdf page from Poppler
gives the same bytes.
Check first with pdfimages -list scan.pdf: this works
when each page is one full-page image, the only kind I tested.
Licenses and installs differ most
| Library | License | Install |
|---|---|---|
| pypdfium2 5.15.2 | Apache-2.0 or BSD-3-Clause, plus the licenses of the PDFium inside | One 3.7 MB wheel, no dependencies |
| PyMuPDF 2.1 | AGPL-3.0 or commercial | 67 MB wheel and 11 more packages |
| pdf2image 1.17.0 | MIT | 11.6 KB wheel, plus Poppler from brew |
pip install pymupdf also installed 11 more packages,
among them onnxruntime (a 22.5 MB wheel), numpy, networkx and
protobuf. For a page renderer, that is a lot of luggage. Wheel sizes
are for a Mac with Apple silicon.
Three traps that cost time
-
use_pdftocairo=Truemade 10 pages take 100.11 s, 50 times slower than without it. It quietly switches pdf2image to PNG, and PNG writing is the slow part. -
pdf2image at 400 dpi peaked at 1,425 MB for 10 pages. Use
first_pageandlast_page, oroutput_folder. - pypdfium2 rounds the size up: 3,080 rows where the others gave 3,079. Fine for OCR, one row off for boxes.
Skip the render and the OCR
PDF OCR to Markdown, our Apify Actor, needs no Poppler or Tesseract on your machine. Digital pages use the PDF's own text.
- Scanned pages: $0.006 each. Digital pages: free. Plus a $0.00005 start fee a run at the default 1 GB.
- On Apify's free plan, it stops at 20 pages a run and 50 a month.
How I tested it
- Scan: 10 proofread book pages from English Wikisource, 1,955 true words, one 1920 px JPEG a page at 300 ppi. Score: true words found in order.
- 7 pt page: a born-digital table PDF with made-up data, one Letter page, most text 7 pt, smallest 6 pt. Truth: its own text layer, 297 words. Score: words found in order, so a table read column by column scores low.
- Each way ran in its own process and rendered all pages 3 times; seconds are the median, 1 worker. Peak MB is the maximum resident set size of that process.
- OCR: Tesseract 5.5.4, eng, default settings, the same command for every image. Rendered pages saved as PNG with no dpi tag; extracted JPEGs as they are.
- Python 3.13.11, Pillow 13.0.0, Poppler 26.03.0, macOS 26.6 on an Apple M5 Max, at background priority. Scripts and results are in research/bench/pdf-to-image-python-2026-11-22/.
What I did not test
- Windows and Linux.
- More than 1 worker for PyMuPDF and pypdfium2.
- Image extraction with pypdfium2.
- Tesseract with a dpi setting, and other OCR engines.
Sources
- pdf2image 1.17.0 on PyPI
- PyMuPDF 2.1 on PyPI
- pypdfium2 5.15.2 on PyPI
- We make the Apify Actor. The three libraries and Poppler are open source, and we do not make them.
Questions people ask
What dpi should I render a PDF at for OCR?
Pass a dpi every time, and try 400 for small print. On a 7 pt page, 400 dpi gave Tesseract 284 to 290 of 297 words. Big book type read about the same at every dpi from 72 to 400.
Does pypdfium2 need Poppler?
No. pypdfium2 is one 3.7 MB wheel with PDFium inside and no dependencies. pdf2image is the one that needs Poppler: brew install poppler on a Mac.
Is PyMuPDF free for commercial use?
Its license is AGPL-3.0, or a paid commercial license from Artifex. In a closed product or a network service, AGPL terms apply unless you buy the commercial license.