Python实现识别图片为文字的示例代码

  

下面我给您详细讲解一下 Python 实现识别图片为文字的示例代码的完整攻略。

准备工作

在开始之前,您需要安装 tesseractpytesseract 两个包。您可以通过以下命令进行安装:

sudo apt install tesseract-ocr
pip install pytesseract

安装完成后,您需要在代码中导入 pytesseract 包,并且指定 tesseract 命令的路径。代码示例:

import pytesseract
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'

加载图片

首先,您需要加载需要识别的图片。这可以通过 PIL 包来完成。代码示例:

from PIL import Image

image_file = Image.open("image.png")

调用识别函数

在加载完图片后,您需要调用 pytesseract 包中的识别函数来进行图片识别。您可以通过如下代码进行调用:

result = pytesseract.image_to_string(image_file)
print(result)

该函数的返回值是字符串类型,表示识别出来的文字内容。在上述示例代码中,我们通过 print 函数将其打印到控制台中。

这里需要注意的是, pytesseract 包中的 image_to_string 函数默认使用英文字符集进行识别,如果您需要识别其他语言的字符,可以通过传递 lang 参数来指定识别语言。例如:

result = pytesseract.image_to_string(image_file, lang='chi_sim')

上述代码中,我们指定了使用中文进行识别。

示例说明

示例一

以下是一个完整的图片识别示例代码,我们将使用 pytesseract 包对一张英文图片进行识别:

from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'

image_file = Image.open("image.png")

result = pytesseract.image_to_string(image_file)

print(result)

示例二

以下是一个使用中文进行识别的示例代码:

from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'

image_file = Image.open("image.png")

result = pytesseract.image_to_string(image_file, lang='chi_sim')

print(result)

以上就是 Python 实现识别图片为文字的示例代码的完整攻略,希望对您有所帮助。

相关文章