使用pytesseract识别图片文字的代码,设置Image
以下是使用pytesseract识别图片文字的代码,其中Image的设置包括图片路径、语言和配置:
import pytesseract
from PIL import Image
# 设置图片路径
img_path = 'example.png'
# 设置语言
lang = 'eng'
# 设置配置
config = '-c tessedit_char_whitelist=0123456789'
# 打开图片并识别文字
img = Image.open(img_path)
text = pytesseract.image_to_string(img, lang=lang, config=config)
# 打印识别结果
print(text)
在本例中,我们使用了Pillow库中的Image模块来打开图片。然后,我们将图片路径、语言和配置传递给pytesseract库中的image_to_string函数来进行文字识别。最后,我们打印出识别结果。
注意,你需要先安装pytesseract和Pillow库,并且需要正确配置tesseract OCR引擎才能使用pytesseract进行图片文字识别。
原文地址: https://www.cveoy.top/t/topic/vVd 著作权归作者所有。请勿转载和采集!