Python OCR 文字识别程序 - 使用 Tesseract 库
以下是使用 Python 和 Tesseract OCR 库实现 OCR 文字识别的示例程序:
import pytesseract
from PIL import Image
# 安装 tesseract 和 pytesseract 库并设置 tesseract 的路径
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# 读取图片文件
img = Image.open('example.jpg')
# 将图片转换为灰度图像
img = img.convert('L')
# 对图像进行二值化处理
threshold = 200
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
img = img.point(table, '1')
# 使用 Tesseract OCR 库识别文本
text = pytesseract.image_to_string(img, lang='eng')
# 输出识别结果
print(text)
该程序首先读取名为 'example.jpg' 的图像文件,然后将其转换为灰度图像,并对其进行二值化处理以增强文本的清晰度。接下来,程序使用 Tesseract OCR 库识别图像中的文本内容,并将识别结果输出到控制台。
原文地址: https://www.cveoy.top/t/topic/mrQo 著作权归作者所有。请勿转载和采集!