惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
Webroot Blog
Webroot Blog
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
云风的 BLOG
云风的 BLOG
IT之家
IT之家
S
Secure Thoughts
U
Unit 42
G
GRAHAM CLULEY
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
The Cloudflare Blog
月光博客
月光博客
V
Visual Studio Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Schneier on Security
Schneier on Security
O
OpenAI News
Hacker News - Newest:
Hacker News - Newest: "LLM"
P
Privacy International News Feed
The Hacker News
The Hacker News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tailwind CSS Blog
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
Last Week in AI
Last Week in AI
C
Check Point Blog
D
Docker
Scott Helme
Scott Helme
Engineering at Meta
Engineering at Meta
博客园_首页
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Vulnerabilities – Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
J
Java Code Geeks
NISL@THU
NISL@THU
S
Security Affairs
C
Cybersecurity and Infrastructure Security Agency CISA
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

Inside Nutrient

A guide to the invisible work behind documents Introducing Nutrient Documents for Salesforce: Native document generation and signing Document AI vs. traditional OCR: Choosing between OCR, AI, and hybrid pipelines PDF SDK compliance and security evaluation checklist for enterprise teams (2026) Invariant Corp replaces paper processes with Nutrient Workflow and scales without limits What is process mapping? A complete guide Nutrient vs. Conga Composer for Salesforce document generation (2026) Document routing: How to automate document distribution The CTO’s AI playbook: Why accountability architecture beats orchestration Compliance workflow automation: Why built-in compliance is table stakes Workflow diagrams: Examples, symbols, and how to build one that actually runs Digital forms: Replace paper forms with automated workflows Approval workflow software: How to automate approvals Why document-centric automation is different The CEO’s AI playbook: Why decision architecture beats model selection Nutrient SDK product updates for Q1 2026 PDF redaction verification: How to prove sensitive data is permanently removed What is a VPAT? The complete guide to accessibility conformance reports What is PDF/UA? The accessible PDF standard explained Salesforce eSignatures: Generate, sign, and track documents in one flow Online document viewer: Options, tradeoffs, and how to embed one Document viewer for web apps: React, Vue, Angular (2026) Best document viewers in 2026: A buyer’s guide How to edit a PDF in Python: Add text, images, and annotations Nutrient advances Workflow platform with agentic AI for enterprise-grade speed and consistency in document-heavy operations How to create a Salesforce quote template from opportunity data The business case for accessibility: Five ways it drives enterprise value Python PDF library comparison (2026): 7 libraries for developers Why your AI agent hallucinates PDF table data PDF.js limitations: When to upgrade to a commercial PDF SDK How Subject scaled 5× with Nutrient’s PDF SDK without rebuilding its document layer I replaced our sales training with an AI coach that runs in Slack — here’s what broke Redirecting to: https://securitybuzz.com/cybersecurity-news/why-enterprise-permissions-are-ais-most-dangerous-inheritance/ Nutrient .NET SDK vs. iText Core: Complete comparison for .NET developers DocuVieware: Support’s most frequently asked setup questions Introducing Nutrient Workflow How to convert PDF to Word in C# (.NET) When email and spreadsheets stop working: Work order approval workflows for field teams on the move Compliance with confidence: Why document-centric automation is the foundation of your mission Nutrient expands AI Assistant, automating multistep document workflows inside any application What is document generation? A developer’s guide to PDF generation Document Converter data flow and how real-time watermarks skip the queue PDF/UA compliance guide: Requirements, standards, and best practices Computers still can’t understand you How Athena Intelligence built AI agents for regulated enterprises with Nutrient’s document infrastructure How to convert HTML to PDF (2026): 4 methods from browser print to SDK How to build a document extraction pipeline with Nutrient Vision API OCR vs. intelligent document processing: Choosing the right document extraction engine Beyond OCR: How document intelligence eliminates manual processing in regulated industries Nutrient vs. IronPDF: Complete comparison for .NET developers Nutrient vs. Aspose.PDF: Complete comparison for .NET developers Redirecting to: https://fortune.com/2026/02/19/openclaw-who-is-peter-steinberger-openai-sam-altman-anthropic-moltbook/ Lufthansa Systems uses Nutrient to deliver reliable, scalable PDF rendering for pilots worldwide Nutrient vs. Syncfusion: Complete comparison for .NET developers React’s useTransition: The hook you’re probably using wrong First City Monument Bank streamlines banking processes with Nutrient Workflow Redirecting to: https://www.sdcexec.com/warehousing/automation/article/22957364/nutrient-workflow-automation-the-missing-link-in-supply-chain-efficiency The complete guide to digital signatures: PAdES, CAdES, and XAdES explained Nutrient Python SDK: Production-grade document processing for Python Introducing agentic document editing for web applications with AI Assistant Nutrient vs. QuestPDF: Complete comparison for .NET developers How we fixed the GdPicture license expiration (and what to do if you’re affected) Red team security testing with agentic AI The future of healthcare document automation Best healthcare workflow software compared Nutrient SDK product updates for Q4 2025 How Harvey scaled legal document workflows 50 percent MoM without rebuilding infrastructure HIPAA-compliant document management in hospitals How we optimized rendering performance while handling thousands of annotations in React — Part 2 Automated PII removal with Nutrient API Redirecting to: https://www.devopsdigest.com/2026-low-code-no-code-predictions Redirecting to: https://www.kmworld.com/Articles/Editorial/ViewPoints/Leaders-predict-AI-to-continue-permeating-all-aspects-of-KM-in-2026-172594.aspx What are deep agents and how do they solve complex problems? Whipping up document magic: Your easy-bake recipe for Vue and Nutrient Web SDK 🧁 What I’ve learned about product iteration planning while building SDKs Passwordless document signing: Three-layer security guide New zip folder functionality streamlines file management in Document Automation Server The keyboard shortcuts playbook: Taking control of keyboard events in Nutrient Web SDK From experienced engineer to AI beginner: My unexpected journey AI-assisted manual testing: Handling Safari’s PDF rendering and UI quirks How to keep a 20-year-old SDK up to date How we optimized rendering performance while handling thousands of annotations in React — Part 1 Nutrient announces new executive hires to accelerate next phase of growth High performance UI using web workers Automate document conversion at scale with Python and Nutrient DCS From curiosity to PLG (and AI): My journey to understanding product-led growth Prost to progress: One year as Nutrient Pigeon usage at Nutrient: Bridging native SDKs to Flutter Modernizing CI build servers: How to migrate from Chef to Ansible Unix man pages: AI-friendly documentation since 1971 Consistent hashing for even load distribution Best AI redaction APIs: Complete comparison guide for 2025 Why AI document redaction matters for modern security From coding to coordinating: How AI transformed my workflow What is intelligent document processing (IDP)? A complete guide Enterprise PDF SDKs: Best PSPDFKit (now Nutrient) alternatives Nutrient SDK product updates for Q3 2025 GdPicture support best practices Redacting sensitive data with Nutrient AI redaction API How AI is transforming the customer experience at Nutrient: From instant answers to intelligent support
Ultimate guide to Python Tesseract
Hulya Masharipov · 2025-02-25 · via Inside Nutrient

Table of contents

    This article explores the various aspects of using Tesseract OCR with Python, covering everything from basic setup and configuration, to advanced techniques such as image preprocessing, multilingual text recognition, and performance optimization. By the end, you’ll have a comprehensive understanding of how to effectively leverage Tesseract OCR in your projects.

    Ultimate guide to Python Tesseract

    Tesseract OCR(opens in a new tab) is widely regarded as one of the most powerful open source optical character recognition (OCR) engines available today. When integrated with Python, it becomes a versatile tool that enables developers to extract text from images with remarkable accuracy.

    What is Tesseract?

    Originally developed by HP and maintained by Google since 2006, Tesseract recognizes more than 100 languages. It’s highly flexible, allowing users to extract text from scanned documents, images, and PDFs. With the right tools, you can integrate Tesseract into your Python projects to automate data extraction and streamline workflows.

    Prerequisites

    Before diving into the implementation of Tesseract OCR with Python, ensure you have the following prerequisites:

    • macOS/Linux — Open a terminal and run:

    sudo apt install python3-pip # For Debian-based Linux

    sudo dnf install python3-pip # For Fedora-based Linux

    brew install python # For macOS (via Homebrew)

    • OpenCV library — This is required for image preprocessing tasks such as grayscale conversion, thresholding, and noise removal. Install it using:

    pip install opencv-python

    • NumPy library — Used for handling numerical operations on image data. Install it using:
    • Pillow — This is a fork of the Python Imaging Library (PIL), which you’ll need to open image files:

    Setting up Tesseract with Python

    Installation

    To get started with Tesseract OCR in Python, you must first install both the Tesseract OCR engine and the pytesseract(opens in a new tab) library, which acts as a wrapper for interfacing with Tesseract from within Python scripts.

    Installing Tesseract

    • Linux — Most Linux distributions include Tesseract in their package repositories, so you can install it using APT with the following command:

    sudo apt install tesseract-ocr

    • For Fedora-based distributions, use:

    sudo dnf install tesseract

    After installation, make sure Tesseract is available in your system’s PATH, or provide the direct path in your code.

    Installing pytesseract

    Once Tesseract is installed on your system, install the pytesseract(opens in a new tab) library using Python’s package manager, pip:

    Verifying installation

    To ensure Tesseract has been installed correctly, open a terminal or command prompt and run the following command:

    If the installation was successful, the terminal will display the version number of Tesseract, along with other relevant details.

    How to use Tesseract with Python

    To demonstrate how Tesseract extracts text from an image, this example uses a sample image containing some printed text.

    Sample image for OCR using Tesseract

    Use this code to get started with OCR using Tesseract and Python:

    from PIL import Image

    import pytesseract

    # Open an image file.

    image = Image.open('example.png')

    # Use pytesseract to do OCR on the image.

    text = pytesseract.image_to_string(image)

    # Print the extracted text.

    print(text)

    In this example:

    • Open an image file using Pillow.
    • Pass the image to pytesseract.image_to_string() to extract the text.

    Terminal output

    Understanding OpenCV (cv2)

    Before working with image preprocessing, it’s essential to understand OpenCV(opens in a new tab), which is commonly used with Tesseract OCR. OpenCV is an open source computer vision library that allows image and video processing in Python. The cv2 module, which is part of OpenCV, provides various functions to manipulate and enhance images before passing them to the OCR engine.

    Key features of OpenCV (cv2)

    • Reading and writing images — OpenCV enables loading, displaying, and saving images using functions like cv2.imread() and cv2.imwrite().
    • Image preprocessing — It provides various image enhancement techniques, such as grayscale conversion, thresholding, noise removal, and edge detection.
    • Geometric transformations — Functions like rotation, resizing, and deskewing help in improving OCR accuracy.
    • Object and text detection — OpenCV supports contour detection, which is useful for extracting regions of interest in images containing text.

    Image preprocessing for better OCR accuracy

    The accuracy of Tesseract OCR is highly dependent on the quality of the input images. Poorly processed images can lead to misinterpretation of characters, reducing the reliability of the extracted text. To mitigate these issues, various image preprocessing techniques can be applied before passing an image to Tesseract for text recognition.

    Grayscale conversion

    Converting an image to grayscale reduces the complexity of the image by eliminating unnecessary color information, making it easier for Tesseract to focus on text detection:

    import cv2

    image = cv2.imread('image.png')

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    cv2.imwrite('gray_image.png', gray)

    Thresholding

    Thresholding(opens in a new tab) is a technique that helps separate text from a background by converting an image into a binary format where pixels are either black or white:

    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

    cv2.imwrite('thresholded_image.png', thresh)

    The code above applies Otsu’s thresholding(opens in a new tab) to a grayscale image, converting it into a binary image where pixels are either black or white. The thresholded result is then saved as thresholded_image.png.

    Noise removal

    Noise, such as small speckles or distortions, can interfere with OCR. The following code, broken down into steps, applies image processing techniques to remove noise and improve text clarity.

    Step 1

    Load and convert to grayscale:

    import cv2

    import numpy as np

    image = cv2.imread('fax.jpg')

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    cv2.imwrite('gray_image.png', gray)

    • Loads the image and converts it to grayscale for simpler processing.

    Step 2

    Apply a bilateral filter:

    denoised = cv2.bilateralFilter(gray, 9, 75, 75)

    • Reduces noise while preserving edges using a bilateral filter.

    Step 3

    Sharpen the image:

    kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])

    sharpened = cv2.filter2D(denoised, -1, kernel)

    • Sharpens the image by enhancing edges with a kernel filter.

    Step 4

    Save the processed image:

    cv2.imwrite('denoised_image.png', sharpened)

    • Saves the final sharpened and denoised image.

    Why does this matter for OCR?

    • Reduces noise, making text more readable.
    • Preserves important details while removing unwanted distortions.
    • Improves OCR accuracy, as clearer text leads to better recognition.

    Deskewing for better OCR accuracy

    After applying thresholding, some scanned documents may have tilted text, affecting OCR accuracy. Deskewing corrects this misalignment, ensuring Tesseract processes the text properly:

    coords = np.column_stack(np.where(thresh > 0)) # Find non-zero pixel coordinates.

    angle = cv2.minAreaRect(coords)[-1] # Calculate the skew angle

    # Adjust the angle for the correct rotation.

    if angle < -45:

    angle = -(90 + angle)

    else:

    angle = -angle

    # Rotate the image to correct the skew.

    (h, w) = thresh.shape[:2]

    center = (w // 2, h // 2)

    M = cv2.getRotationMatrix2D(center, angle, 1.0)

    deskewed = cv2.warpAffine(thresh, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

    cv2.imwrite('deskewed_image.png', deskewed)

    By detecting the text angle and rotating the image, this method straightens skewed text, improving OCR results.

    Multilingual text recognition

    Tesseract supports multiple languages, making it a valuable tool for extracting text in different scripts and dialects. To enable multilingual OCR, the required language data files must be correctly installed and configured.

    Language data files

    Tesseract provides language data files that can be downloaded from Tesseract’s language repository(opens in a new tab) and placed in the tessdata directory of the Tesseract installation.

    Configuring language in pytesseract

    To instruct Tesseract to recognize multiple languages in an image, specify the desired languages in the lang parameter of pytesseract.image_to_string():

    import pytesseract

    text = pytesseract.image_to_string(image, lang='eng+fra')

    print(text)

    Advanced configuration options

    Page segmentation modes (PSM)

    Tesseract offers various page segmentation modes (PSM)(opens in a new tab) that determine how text is segmented within an image. For example:

    text = pytesseract.image_to_string(image, config='--psm 6')

    Common PSM modes include:

    • 3 — Fully automatic page segmentation.
    • 6 — Assumes a single uniform block of text.
    • 11 — Sparse text with no predefined order.

    OCR engine modes (OEM)

    Tesseract supports multiple OCR engine modes (OEM), which define the underlying text recognition approach:

    text = pytesseract.image_to_string(image, config='--oem 1')

    Common OEM modes include:

    • 0 — Uses the legacy OCR engine only.
    • 1 — Uses the LSTM-based OCR engine.
    • 3 — Uses a combination of the legacy and LSTM engines.

    Allowed and disallowed characters

    To improve accuracy, Tesseract allows restricting recognition to specific characters:

    custom_config = r'--psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

    text = pytesseract.image_to_string(image, config=custom_config)

    Tesseract can extract text from PDFs by converting them to images. Ensure you have Poppler(opens in a new tab) installed first.

    Installing Poppler

    First, install Poppler:

    Then, use this code:

    import pytesseract

    from pdf2image import convert_from_path

    # Convert PDF to images.

    images = convert_from_path('example.pdf')

    # Extract text from the first page.

    text = pytesseract.image_to_string(images[0])

    print(text)

    You’ll need to install pdf2image using:

    Tips for improving OCR accuracy

    1. Use higher-quality images — The better the quality of the image, the more accurate the OCR.
    2. Preprocess images — Use techniques such as thresholding, contrast adjustment, and noise removal to enhance the text’s visibility.
    3. Use Tesseract configuration options — Tesseract provides several configuration options for fine-tuning the OCR process. For example:

    custom_config = r'--oem 3 --psm 6'

    text = pytesseract.image_to_string(image, config=custom_config)

    • --oem 3 tells Tesseract to use the default OCR engine mode.
    • --psm 6 sets the page segmentation mode for better layout analysis.

    Common OCR challenges and how to solve them

    1. Low-quality images

    To deal with blurry or noisy images, apply preprocessing steps like:

    • Denoising — cv2.fastNlMeansDenoising()
    • Binarization — cv2.threshold()

    2. Text in different fonts or styles

    Tesseract may struggle with complex fonts or distorted text. In such cases, try training Tesseract on custom fonts, or use additional image processing to make the text clearer.

    3. Non-standard characters

    If Tesseract isn’t recognizing special characters or symbols, ensure you have the correct language data and adjust the OCR engine settings for better accuracy.

    Nutrient OCR solutions and features

    Nutrient provides advanced OCR capabilities across multiple platforms, enabling text extraction from scanned PDFs and images. These features help make documents searchable, editable, and more accessible.

    Nutrient products supporting OCR

    1. Web SDK — Supports OCR when used with Document Engine in server-backed mode.
    2. .NET SDK — Provides OCR functionality for Windows applications.
    3. iOS SDK — Enables text recognition in iOS apps.
    4. Android SDK — Offers OCR capabilities for Android applications.
    5. Mac Catalyst — Supports OCR for macOS development.
    6. React Native SDK — Allows OCR integration in cross-platform mobile apps.
    7. Flutter SDK — Provides OCR features for Flutter-based applications.
    8. Java SDK — Includes robust OCR support for Java applications.
    9. Document Converter — Adds OCR functionality for SharePoint and server-based conversions (available from version 7.1).
    10. Nutrient API for OCR — Check out our Using Tesseract OCR with Python for image text extraction blog post for implementation details.

    Key features of Nutrient OCR products

    • Multi-language support — Recognizes text in multiple languages for global usability.
    • Searchable PDFs — Converts scanned documents into searchable and selectable text.
    • High accuracy — Uses advanced text recognition algorithms for precise extraction.
    • Layout retention — Preserves document structure, including tables and columns.
    • Batch processing — Processes multiple pages and documents efficiently.
    • Integration with AI/ML — Enhances text extraction with AI-powered features.
    • Cross-platform compatibility — Supports various platforms, including Web, .NET, iOS, Android, React Native, Flutter, Java, and macOS.
    • SharePoint and server support — Available in Document Converter for server-based and SharePoint OCR workflows.

    OCR may require an additional license component. For more details, visit Nutrient OCR solutions or contact Sales.

    Conclusion

    Tesseract OCR, when combined with Python, provides an efficient and highly customizable solution for text recognition. By applying proper image preprocessing techniques, configuring Tesseract effectively, and optimizing performance, developers can achieve exceptional OCR accuracy. Whether your goal is automating data entry, digitizing historical documents, or developing real-time text recognition applications, Tesseract Python offers the necessary flexibility and power to handle a wide range of OCR tasks.

    For those seeking even more advanced OCR solutions, Nutrient offers a suite of robust, multi-platform products that enhance and extend OCR capabilities. With features like searchable PDFs, batch processing, AI/ML integration, and cross-platform compatibility, Nutrient products provide an ideal complement to Tesseract. To discover how Nutrient can transform your OCR workflows and address your specific needs, contact Sales for more information.

    Related Python guides

    OCR and document processing

    FAQ

    Tesseract OCR leverages advanced image processing and recognition algorithms to extract text from images. When combined with Python libraries like pytesseract, it provides a streamlined process for converting images and scanned documents into editable text.

    Enhancing Tesseract OCR accuracy involves using high-quality images and applying preprocessing techniques such as grayscale conversion, thresholding, and noise removal. Additionally, fine-tuning configurations like page segmentation modes (PSM) and OCR engine modes (OEM) can lead to significantly improved results.

    Nutrient provides a suite of OCR products that extend the capabilities of Tesseract by integrating advanced features like batch processing, AI-powered text recognition, and seamless multi-platform support. These enhancements simplify document management and boost overall processing efficiency.

    Nutrient Web SDK is tailored for web-based OCR applications. It integrates smoothly with Tesseract OCR through our robust Document Engine, offering a user-friendly interface and reliable performance for extracting text from web-delivered documents and images.

    The Nutrient API for OCR offers a scalable and flexible solution for automating text extraction. It supports multiple platforms, simplifies complex document workflows, and provides advanced configuration options that allow businesses to optimize their OCR processes.

    Explore related topics

    Try for free Ready to get started?

    Related SDK articles

    Explore more