How can Python be used to recognize text in images?
An example code using the Tesseract library for OCR recognition can be used in Python to identify text in images.
import pytesseract
from PIL import Image
# 读取图片
image = Image.open('image.png')
# 使用Tesseract进行OCR识别
text = pytesseract.image_to_string(image, lang='eng')
# 输出识别的文字
print(text)
In the code above, the first step is to install the Tesseract and Pillow libraries.
pip install pytesseract
pip install Pillow
Next, open the image using the Image.open function and use the pytesseract.image_to_string function to recognize the text in the image. The lang parameter can specify the language for recognition, defaulting to English.
It is important to note that using Tesseract for OCR recognition requires installing the Tesseract OCR engine beforehand. For specific installation methods, refer to the official documentation provided by Tesseract.