OCR¶
Overview¶
PyMuPDF includes built-in OCR support for scanned documents and image-based PDFs. By default, OCR runs automatically when needed — you don’t have to opt in. For more control, you can force OCR on specific pages, disable it entirely, or swap in a different OCR engine using the adaptor interface.
PyMuPDF will use RapidOCR and/or Tesseract engines depending on availability and suitability for the task.
OCR Engine Comparison¶
We compared both OCR engines accuracy on the ExtractBench document corpus on a set of 370 various types of documents:
PyMuPDF + Tesseract |
PyMuPDF + RapidOCR |
|---|---|
78.31% |
85.20% |
Overall, for the best OCR accuracy, it is recommended to use RapidOCR.
Tip
OCR requires a working installation. See OCR Installation for setup instructions.
Hybrid OCR strategy¶
PyMuPDF applies OCR only when it is genuinely required to obtain the complete text of a PDF page. If a page already contains sufficient extractable text, OCR is skipped entirely — avoiding unnecessary work and eliminating the risk of degrading high-quality digital text.
When OCR is needed, PyMuPDF automatically selects the most suitable OCR plugin available in the runtime environment, balancing detection accuracy with processing speed.
Its built-in OCR plugins implement a Hybrid OCR strategy: only those regions lacking extractable, legible text are passed to the OCR engine. This selective approach typically reduces OCR processing time by around 50% while improving recognition accuracy, since the engine focuses exclusively on the problematic regions. The recognized text is then merged back into the original page, enriching it without disturbing existing digital content.
Auto-OCR Behaviour¶
PyMuPDF inspects each page before extracting text. If a page contains no selectable text — meaning all content is rasterised into images — OCR is triggered automatically for that page.
Pages that contain native text are never sent through OCR, even if they also contain embedded images. This keeps processing fast and avoids degrading already-clean text.
import pymupdf
# OCR runs automatically on any page with no selectable text
doc = pymupdf.open("scanned-document.pdf")
md_text = doc.to_markdown()
The resulting Markdown is seamless — pages extracted via OCR and pages extracted natively are combined into a single output with no distinction between them.
How OCR is Triggered¶
There are two scenarios where OCR is applied automatically:
No text at all — if a page contains roughly no text but is covered with images or many character-sized vectors, PyMuPDF checks whether text is probably detectable on the page. This distinguishes image-based text (e.g. a scanned document) from ordinary pictures like photographs.
Garbled text — if a page does contain text but too many characters are unreadable (e.g. text extraction results in “�” replacements for the characters), OCR is applied for the affected text areas only, not the full page. This preserves already-readable text, images, and vectors while recovering only what is broken.
Note
For these heuristics to work, at least one of the supported default OCR engines, Tesseract or RapidOCR must be installed. Otherwise, the behavior is the same as if use_ocr=False was specified. If you have your own, non-default OCR engine, you must supply the respective plugin.
Decision Tree¶
OCR is applied only when all four of the following conditions are met:
PyMuPDF Layout is imported — The Layout analysis module must be active. This is
Trueby default.use_ocr is enabled — The
use_ocrparameter in the PyMuPDF API must beTrue. This is the default value.Tesseract is installed — Tesseract must be correctly installed on your system. See OCR installation.
OpenCV is available — The
opencv-pythonpackage must be available in your Python environment.
Forcing OCR¶
In some cases you may want to force OCR even on pages that contain selectable text — for example, when the native text layer is corrupt, misencoded, or misaligned with the visual content.
Use force_ocr=True to bypass the auto-detection check entirely:
doc = pymupdf.open("document.pdf")
md_text = doc.to_markdown(force_ocr=True)
Warning
Forcing OCR on clean, text-based PDFs will slow down processing significantly and may reduce output quality. Only use force_ocr=True when you have reason to distrust the native text layer.
You can also force OCR on specific pages rather than the whole document:
doc = pymupdf.open("document.pdf")
md_text = doc.to_markdown(
pages=[2, 3, 4],
force_ocr=True
)
Disabling OCR¶
To prevent OCR from running at all — even on pages with no selectable text — set use_ocr=False:
doc = pymupdf.open("document.pdf")
md_text = doc.to_markdown(use_ocr=False)
Pages with no selectable text will return empty strings in this mode. This is useful when you know your documents are always text-based, or when you want to handle OCR yourself in a downstream step.
OCR Adaptors¶
By default, PyMuPDF uses Tesseract and internal models for image pre-processing. If you need a different OCR engine you can plug in a custom adaptor.
Built-in Adaptors¶
RapidOCR¶
If RapidOCR and the RapidOCR ONNX Runtime are available, you can use a pre-made callable OCR function for it, which is provided in the pymupdf4llm.ocr module as rapidocr_api.exec_ocr.
Example
from pymupdf4llm.ocr import rapidocr_api
doc = pymupdf.open("document.pdf")
md = doc.to_markdown(
ocr_function=rapidocr_api.exec_ocr,
force_ocr=True
)
In this way RapidOCR can be used as an alternative OCR engine to Tesseract for all pages (if force_ocr=True) or just for those pages which meet the default criteria for applying OCR (if force_ocr=False or omitted).
RapidOCR & Tesseract Side-by-Side¶
If both OCR engines are installed, Tesseract and one of RapidOCR / RapidOCR-ONNXRuntime, they can be combined together.
The following conditions apply:
RapidOCR version is
rapidocr_onnxruntime& Tesseract is available then they are combined together by default.RapidOCR 3.9.1 or later: no combination by default, but can be combined with Tesseract if required.
This pre-made default callable OCR function can be found in the pymupdf4llm.ocr module as rapidtess_api.exec_ocr.
Example
from pymupdf4llm.ocr import rapidtess_api
doc = pymupdf.open("document.pdf")
md = doc.to_markdown(
ocr_function=rapidtess_api.exec_ocr,
force_ocr=True
)
Adaptor |
Engines |
Notes |
|---|---|---|
|
RapidOCR or RapidOCR-ONNXRuntime |
Requires RapidOCR and ONNX Runtime |
|
RapidOCR & Tesseract |
Better accuracy for bounding box detection and text recognition |
Writing a Custom Adaptor¶
An OCR adaptor can be defined by passing your own Python ocr_function method which follows a protocol as follows:
def exec_ocr(page, dpi=300, pixmap=None, language="eng", keep_ocr_text=False):
"""
Custom OCR function to replace the default Tesseract-based implementation.
Parameters:
- page: The PyMuPDF page object being processed.
- dpi: The resolution at which to render the page for OCR.
- pixmap: An optional pre-rendered pixmap of the page, if available.
- language: The language to be used for OCR.
- keep_ocr_text: Whether to keep text from a previous OCR of the page.
If a Pixmap is provided, the DPI parameter is ignored. Otherwise, an RGB
Pixmap is created from the page at the specified DPI.
"""
# Your custom OCR logic here.
# The method should render the OCR'ed text onto the page
# so that PyMuPDF can extract it as usual.
...
Tip
Custom adaptors receive a page or image object and must render what they “see” onto the source document. PyMuPDF handles interpretation — your adaptor needs to handle the text recognition & rendering step.
OCR Language Support¶
When using the default Tesseract adaptor, you can specify one or more languages using Tesseract’s language codes.
Specify the language to be used by the Tesseract OCR engine. Default is "eng" (English). Make sure that the respective language data files are installed. Remember to use correct Tesseract language codes. Multiple languages can be specified by concatenating the respective codes with a plus sign "+", for example "eng+deu" for English and German.
doc = pymupdf.open("multilingual.pdf")
md_text = doc.to_markdown(ocr_language="eng+deu")
Tesseract language packs must be installed separately on your system. For example, on Ubuntu:
sudo apt install tesseract-ocr-deu tesseract-ocr-fra
See the page on installing Tesseract language packs for further details.
When RapidOCR is used for text recognition (adaptor rapidocr_api), no language packs are needed. The same language codes then select a RapidOCR recognition model for the following scripts:
Script |
Language codes |
|---|---|
Korean |
|
Greek |
|
Cyrillic |
|
doc = pymupdf.open("korean-scan.pdf")
md_text = doc.to_markdown(ocr_language="kor")
Note
RapidOCR uses one recognition model per page. If several codes are specified, the first one from the table selects the model — for example Korean for "eng+kor". RapidOCR downloads the model when it is first used. All other codes, including the default "eng", use RapidOCR’s default model.
How to OCR an Image¶
A supported image must first be converted to a Pixmap. The Pixmap can then be saved to a 1-page PDF. This page will look like the original image with the same width and height. It will contain a layer of text as recognized by Tesseract.
The PDF can be generated via one of the methods Pixmap.pdfocr_save() or Pixmap.pdfocr_tobytes(), as a file on disk or as a PDF in memory.
The text can be extracted and searched with the usual text extraction and search methods (Page.get_text(), Page.search_for(), etc.). Please also note the following important facts and prerequisites:
When converting the image to a Pixmap, please confirm that the color space is RGB and alpha is
False(no transparency). Convert the original Pixmap if necessary.All text is written as “hidden” with Tesseract’s own
GlyphLessFont, a mono-spaced font with metrics comparable to Courier.All text has the properties regular and black (i.e. no bold, no italic, no information about the original fonts).
Tesseract does not recognize vector graphics (i.e. no drawings / line-art).
This approach is also recommended to OCR a complete scanned PDF:
Render each page to a Pixmap with desired resolution
Append the resulting 1-page PDF to the output PDF
How to OCR a Document Page¶
Any supported document page can be OCR-ed – either the complete page or only the image areas on it.
Because optical character recognition is about one thousand times slower than standard text extraction, we make sure to do OCR only once per page and store the result in a TextPage. Using this TextPage for all subsequent extractions and text searches will then happen with PyMuPDF’s usual top speed.
To OCR a document page, follow this approach:
Determine whether OCR is needed / beneficial at all. A number of criteria can be used for this decision, like:
page is completely covered by an image
no text exists on the page
thousands of small vector graphics (indicating simulated text)
Performance Tips¶
OCR is the most compute-intensive part of the extraction pipeline. A few ways to keep it fast:
Process only the pages you need using the
pagesparameter to avoid running OCR on the entire document.Cache results — write the output to disk after the first run so you don’t re-process the same file.
Use
force_ocr=False(the default) so clean pages skip OCR entirely.Resize images before passing to OCR — very high DPI scans can slow Tesseract down without improving accuracy.
