python的ocr模块
导读:Python的OCR模块可以帮助程序员在Python程序中实现图像文字的识别和提取。在OCR模块中,最为流行的是使用开源库Tesseract来实现OCR功能。import pytesseractfrom PIL import Imagepy...
Python的OCR模块可以帮助程序员在Python程序中实现图像文字的识别和提取。在OCR模块中,最为流行的是使用开源库Tesseract来实现OCR功能。
import pytesseractfrom PIL import Imagepytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # 设置tesseract的路径def ocr(image_path):image = Image.open(image_path) # 打开图片text = pytesseract.image_to_string(image, lang='chi_sim') #提取图像中的文字return textif __name__ == '__main__':text = ocr('example.png')print(text)
在该样例中,程序首先需要导入pytesseract和Pillow库。接下来设置tesseract的路径,将要读取图片传入函数中实现OCR功能,在OCR函数中,使用pytesseract.image_to_string函数来识别图像中的文字,设置lang参数表示识别语言为中文,然后返回提取的文字。最后,在main函数中,读取示例图片example.png,调用OCR函数进行文字识别,并打印提取的文字。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: python的ocr模块
本文地址: https://pptw.com/jishu/514102.html