Python「pytesseract」:中文识别模块

在处理 .ttf 文件时,遇到了识别图片中中文的情况,常见的方式是调用百度的语言识别接口,但是这里为了大批量的识别,首先试了试 python 自带的语言识别模块 pytesseract ,这里简单做一下记录。

目录

一、介绍

(一)image_to_string

(二)config参数 

二、代码


一、介绍

(一)image_to_string

pytesseract.image_to_string

def image_to_string(
    image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0,
):
    """
    Returns the result of a Tesseract OCR run on the provided image to string
    将在提供的图像上运行的Tesseract OCR的结果返回给string
    """

参数含义: 

  • image Object or String - PIL Image/NumPy array or file path of the image to be processed by Tesseract. If you pass object instead of file path, pytesseract will implicitly convert the image to RGB mode.

对象或者字符串。PIL Image/NumPy 数组或者是Tesseract处理图像的文件路径。如果传来的是一个对象的话,会把图像转换成RGB格式。

  • lang String - Tesseract language code string. Defaults to eng if not specified! Example for multiple languages: lang='eng+fra'

语言码。默认自然是英语(eng),这里中文简体为chi_sim,当然也可以采用多种语言。

  • config String - Any additional custom configuration flags that are not available via the pytesseract function. For example: config='--psm 6'

任何通过pytesseract函数不可用的额外自定义配置标志。

  • nice Integer - modifies the processor priority for the Tesseract run. Not supported on Windows. Nice adjusts the niceness of unix-like processes.

修改Tesseract运行的处理器优先级。不支持windows系统。Nice调整类unix系统进程的良好性。

  • output_type Class attribute - specifies the type of the output, defaults to string. For the full list of all supported types, please check the definition of pytesseract.Output class.

输出的特定方式,默认是string。

  • timeout Integer or Float - duration in seconds for the OCR processing, after which, pytesseract will terminate and raise RuntimeError.

OCR处理的持续时间(以秒为单位),在此之后,pytesseract将终止并引发RuntimeError。

(二)config参数 

 这里详细介绍一下 config 参数,通过命令行执行 tesseract --help-psm 命令,我们可以看到

$ tesseract --help-psm
Page segmentation modes:
  0    Orientation and script detection (OSD) only.

方向和脚本检测(OSD)。
  1    Automatic page segmentation with OSD.

自动页面分割与OSD。
  2    Automatic page segmentation, but no OSD, or OCR.

自动页面分割,但没有OSD,或OCR。
  3    Fully automatic page segmentation, but no OSD. (Default)

全自动页面分割,但没有OSD。(默认)
  4    Assume a single column of text of variable sizes.

假设有一列大小不同的文本。
  5    Assume a single uniform block of vertically aligned text.

假设有一个垂直对齐的文本块。
  6    Assume a single uniform block of text.

包含一个统一的文本块。
  7    Treat the image as a single text line.

将图像重新定义为单个文本行。
  8    Treat the image as a single word.

将图像视为一个单词。
  9    Treat the image as a single word in a circle.

将图像视为一个圆圈中的单个单词。
 10    Treat the image as a single character.

将图像视为单个字符。
 11    Sparse text. Find as much text as possible in no particular order.

稀疏的文本。在没有特定顺序的情况下,尽可能多地查找文本。
 12    Sparse text with OSD.

稀疏文本与OSD。
 13    Raw line. Treat the image as a single text line,
       bypassing hacks that are Tesseract-specific.

将图像视为一个单独的文本行,绕过特定于tesseract的技巧。

二、代码

使用如下所示:

import pytesseract
from PIL import Image


# 识别
text = pytesseract.image_to_string(Image.open('图片路径'), lang='chi_sim', config='-psm 6')

 

  • 11
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好! Python Pytesseract 是一个 Python 模块,它提供了一个简单的 API,可以让您在 Python 中使用 Tesseract OCR 引擎来识别图像中的文字。 要使用 pytesseract,您需要先安装 Tesseract OCR 引擎和 pytesseract 模块。您可以使用以下命令在终端中安装这些软件包: ``` sudo apt-get install tesseract-ocr pip install pytesseract ``` 安装完成后,您可以使用以下代码来识别图像中的文字: ```python import pytesseract from PIL import Image # 读取图像 img = Image.open('example.png') # 调用 pytesseract 模块进行 OCR 识别 text = pytesseract.image_to_string(img, lang='chi_sim') # 打印识别结果 print(text) ``` 请注意,这个例子假设您的图像文件名为 "example.png",且该文件位于当前工作目录中。另外,lang 参数指定要使用的语言,这里我们选择了简体中文(chi_sim)。 希望这可以帮到您! ### 回答2: Python pytesseract是一个OCR库,是Tesseract-OCR引擎的Python封装。OCR是光学字符识别(Optical Character Recognition)的缩写,是一种将印刷或手写文本转换为可编辑电子文本的技术。Python pytesseract库可用于对图片或者扫描文档中的字符进行识别识别的结果可以输出为文本格式,方便后续的处理。 Python pytesseract简单易用,只需import库并指定图片路径即可,支持多种图像格式,包括jpg、png、bmp、tiff等。使用时需要先安装好Tesseract-OCR引擎,安装Tesseract-OCR时需将其路径添加到环境变量中。 Python pytesseract库提供了一些可选参数,可以对OCR的识别结果进行优化。例如,可以指定识别语言、调整识别文本的方向、去除非文本的干扰等。此外,还可以指定输出格式(默认输出方式为纯文本格式),输出hocr格式的HTML文件、ALTO格式的XML文件等。 Python pytesseract的应用范围非常广泛,比如自动化填表、文档自动化处理、文字图像识别、手写文字识别等等。特别是在机器视觉领域,Python pytesseract的应用可以简化很多操作,大幅提高效率,节省时间和精力。 总之,Python pytesseract是一个功能强大、使用简便的OCR库,为我们日常的文字识别任务提供了很大的便利。 ### 回答3: Python pytesseract是一个用于OCR(Optical Character Recognition, 光学字符识别)的Python库。它是Tesseract-OCR引擎的一个包装器,能够识别许多不同类型的图像和文字,包括数字、字母、符号、汉字等多种语言。因为它是一个Python库,所以使用它进行OCR非常方便,而且可以在大多数平台上使用。 Python pytesseract使用起来非常简单,只需一些基本的Python编程知识即可。安装Python pytesseract需要先安装Tesseract OCR引擎,可以在Tesseract OCR官网上进行下载。然后,安装Python pytesseract库,方法很简单,可以通过pip工具进行安装,使用命令pip install pytesseract即可。 使用Python pytesseract进行OCR时,先将需要识别的图像打开,并将其转换成灰度图像,以便更好地识别字符。然后,调用Pytesseract库中的image_to_string函数进行字符识别,并传入灰度图像进行处理。函数的返回值是一个字符串,包含从图像中识别出的所有字符。如果需要提高识别精度,可以调整image_to_string函数的参数,例如指定识别的语言、字符集、识别区域、字符过滤等等。 Python pytesseract是一个非常实用和强大的库,可以在各种OCR应用场合中发挥重要作用。无论是自动化办公、图像识别、数据挖掘、机器学习、自然语言处理等等,都需要有一个强大的OCR工具来支持。Python pytesseract不仅提供了OCR的基本功能,而且具有良好的可扩展性和灵活性,可以满足各种OCR应用的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值