Python中可以使用OCR(Optical Character Recognition,光学字符识别)库来实现识别图片中的文字。下面是一个使用Tesseract库进行OCR识别的示例代码:
import pytesseractfrom PIL import Image# 读取图片image = Image.open('image.png')# 使用Tesseract进行OCR识别text = pytesseract.image_to_string(image, lang='eng')# 输出识别的文字print(text)
在上面的代码中,首先需要安装Tesseract库和Pillow库:
pip install pytesseractpip install Pillow
然后,通过Image.open
函数打开图片,并使用pytesseract.image_to_string
函数将图片中的文字识别出来。lang
参数可以指定识别的语言,默认为英语。
需要注意的是,使用Tesseract进行OCR识别需要提前安装Tesseract OCR引擎。具体安装方法可以参考Tesseract的官方文档。