Pix2Text (P2T) Documentation
repository·main·Indexed 25 days ago
https://github.com/breezedeus/pix2textAn open-source Python toolkit for converting images and PDFs into Markdown. Pix2Text specializes in recognizing complex layouts, tables, text, and mathematical formulas, serving as an alternative to Mathpix. It supports over 80 languages using CnOCR for English and Simplified Chinese, and EasyOCR for others. The toolkit provides a Python API, a CLI with `p2t predict` and `p2t serve` commands, and a MacOS desktop application.
What's inside Pix2Text
- Pix2Text (P2T) is a free and open-source Python toolkit designed as an alternative to Mathpix. It can recognize layouts, tables, images, text, and mathematical formulas, integrating them into Markdown format. P2T also supports converting entire PDF files (including scanned images) into Markdown.
Overview of Pix2Text (P2T) capabilities
mainPix2Text (P2T) is an open-source Python tool designed as a free alternative to Mathpix. It can recognize layouts, tables, images, text, and mathematical formulas within images and integrate them into a single Markdown output. It also supports converting entire PDF files (including scanned images) into Markdown format.Install Pix2Text via pip
mainInstall the core Pix2Text package using pip.
If you need to recognize languages other than English and Simplified Chinese, install the
multilingualextra to include necessary dependencies for EasyOCR.If you are in a region with slow access to PyPI, you can use a mirror like Aliyun.
Automatic Model Downloading
mainOn the first use of Pix2Text, the system automatically downloads the required open-source models.
- Pix2Text models are stored in
~/.pix2text(Windows:C:\Users\<username>\AppData\Roaming\pix2text). - CnOCR models are stored in
~/.cnocr(Windows:C:\Users\<username>\AppData\Roaming\cnocr). - CnSTD models are stored in
~/.cnstd(Windows:C:\Users\<username>\AppData\Roaming\cnstd).
If the automatic download fails due to network restrictions, you can manually download models from huggingface.co/breezedeus (or hf-mirror.com/breezedeus) and place them in the corresponding directories.
- Pix2Text models are stored in
Install Pix2Text
mainInstall the core Pix2Text package using pip. For basic usage (English and Simplified Chinese), use the standard install command. If you need to support languages other than English and Simplified Chinese, install the
multilingualextra.To speed up installation in certain regions, you can specify a mirror like Aliyun.
Recognize PDF files and export to Markdown
mainUse the
.recognize_pdf()method to process entire PDF files or specific pages. The results can be exported to a Markdown file using the.to_markdown()method on the returned document object.To use the CLI for PDF recognition, set
--file-type pdfand use--rec-kwargs '{"page_numbers": [0, 1]}'to specify pages.from pix2text import Pix2Text img_fp = './examples/test-doc.pdf' p2t = Pix2Text.from_config() doc = p2t.recognize_pdf(img_fp, page_numbers=[0, 1]) doc.to_markdown('output-md') # The exported Markdown information is saved in the output-md directoryUse the P2T Web Version for Chinese and English
mainThe P2T Web Version is a free service available for everyone. It supports up to 10,000 free character recognitions per person per day.
Key details:
- Supported Languages: Currently limited to Simplified Chinese and English due to machine resource constraints.
- Usage Policy: Please avoid batch calling the interface to ensure service availability for others.
- Connectivity: If the site cannot be opened, you may need to use a proxy/VPN (scientific internet access).
Recognize pure text images
mainUse the
.recognize_text()method for images containing only text (no formulas). This treats Pix2Text as a general OCR engine and returns the recognized text as a string.In the CLI, use
--file-type text.from pix2text import Pix2Text img_fp = './examples/general.jpg' p2t = Pix2Text.from_config() outs = p2t.recognize_text(img_fp) print(outs)Recognize pure formula images as LaTeX
mainUse the
.recognize_formula()method for images containing only mathematical formulas. It returns the formula as a LaTeX expression string.In the CLI, use
--file-type formula.from pix2text import Pix2Text img_fp = './examples/math-formula-42.png' p2t = Pix2Text.from_config() outs = p2t.recognize_formula(img_fp) print(outs)Install the Pix2Text Mac Desktop Client
mainFor macOS users, a dedicated desktop client is available via thePix2Text-Macrepository.Use Pix2Text Web Version or Online Demo
mainIf you prefer not to use Python locally, you can use the following web-based options:
- P2T Web Version: https://p2t.breezedeus.com. Best for Simplified Chinese and English. It offers 10,000 free characters per day.
- Online Demo: https://huggingface.co/spaces/breezedeus/Pix2Text-Demo. Best for testing other languages. (Mirror available: https://hf.qhduan.com/spaces/breezedeus/Pix2Text-Demo).
Manually install the Mathematical Formula Recognition (MFR) model
mainIf the MFR model fails to download automatically, download it from breezedeus/pix2text-mfr (or use the domestic mirror).
Place all downloaded files into the following directory:
- Linux/macOS:
~/.pix2text/1.1/mfr-1.5-onnx - Windows:
C:\Users\<username>\AppData\Roaming\pix2text\1.1\mfr-1.5-onnx
Note: Replace
1.1with your specific Pix2Text version if it differs.- Linux/macOS: