Pix2Text (P2T) Documentation

repository·main·Indexed 25 days ago

https://github.com/breezedeus/pix2text

An 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.

Tokens
13K
Snippets
33
Records
68
Agent score
82%

What's inside Pix2Text

  1. Overview of Pix2Text (P2T)

    main
    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.
  2. Overview of Pix2Text (P2T) capabilities

    main
    Pix2Text (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.
  3. Install Pix2Text via pip

    main

    Install the core Pix2Text package using pip.

    If you need to recognize languages other than English and Simplified Chinese, install the multilingual extra to include necessary dependencies for EasyOCR.

    If you are in a region with slow access to PyPI, you can use a mirror like Aliyun.

  4. Automatic Model Downloading

    main

    On 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.

  5. Install Pix2Text

    main

    Install 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 multilingual extra.

    To speed up installation in certain regions, you can specify a mirror like Aliyun.

  6. Recognize PDF files and export to Markdown

    main

    Use 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 pdf and 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 directory
  7. Use the P2T Web Version for Chinese and English

    main

    The 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).
  8. Recognize pure text images

    main

    Use 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)
  9. Recognize pure formula images as LaTeX

    main

    Use 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)