Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Python and Tesseract to recognize picture text

Shulou Source: shulou.com Published: 2022-06-01 20:24:01 10月02日 Update

This article is about how to use Python and Tesseract to recognize pictures and text. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Installation

The installation of Linux and Mac platform is very simple. You can install it directly on the command line. By default, only English language packs are available, while Chinese language packs need to be specified.

Sudo apt-get install tesseract-ocr # ubuntu

Brew install tesseract # macOS

The Windows platform requires a binary installation package. The official download address is https://github.com/tesseract-ocr/tesseract/wiki/4.0-with-LSTM#400-alpha-for-windows. When installing, you need to select a Chinese language package to recognize Chinese characters.

Configuration

After the installation is completed, you need to set two environment variables $PATH and $TESSDATA_PREFIX. If you do not specify an error, add the installation path of tessertact to the PATH variable, and specify the value of the TESSDATA_PREFIX variable as the path of the language pack.

Actual combat

Python-tesseract is tessertact's Python package, which relies on the image processing library PIL (Pillow). After the environment is built, you can start customized development.

Below, I use du Fu's "climbing" as a demo to extract the text from it.

# pip install pytesseract install dependency package first

Try:

Import Image

Except ImportError:

From PIL import Image

Import pytesseract

# lang specifies simplified Chinese

Text = pytesseract.image_to_string (Image.open ('dufu-denggao1.jpeg'), lang='chi_sim')

Print (text)

Output result:

The wind is strong and the ape whistle is sad, and the gulls and herons on the white sand bank of Qingqing Hezhou fly back at low altitude. The fallen leaves are scattered one after another one after another, and the Yangtze River is surging and surging. As a guest in thousands of miles, I often wander everywhere. I have been sick all my life and have climbed the stage alone today. Times are difficult, life is difficult, often hate temples as white as frost, overpour Xinding muddy wine cup.

Thank you for reading! On "how to use Python and Tesseract to identify picture text" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: Pictures text variables language content platform more environment article path Chinese good practical two binary only command address official Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno NVidia Shulou Tech Info MySQL