图像文本python

时间:2016-07-21 14:47:13

标签: python python-3.x pytesser

我正在使用python 3.x并使用以下代码将图像转换为文本:

from PIL import Image
from pytesseract import image_to_string

image = Image.open('image.png', mode='r')
print(image_to_string(image))

我收到以下错误:

Traceback (most recent call last):
  File "C:/Users/hp/Desktop/GII/Image_to_text.py", line 12, in <module>
    print(image_to_string(image))
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
    config=config)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

请注意,我已将图像放在我的python所在的同一目录中。此外,它不会在image = Image.open('image.png', mode='r')上引发错误,但会在print(image_to_string(image))行上引发错误。

知道这里可能有什么问题吗?感谢

5 个答案:

答案 0 :(得分:4)

您必须在路径中安装tesseract并可访问。

According to sourcepytesseract仅仅是subprocess.Popen的包装器,其中tesseract二进制文件作为要运行的二进制文件。它不会执行任何类型的OCR。

相关部分来源:

def run_tesseract(input_filename, output_filename_base, lang=None, boxes=False, config=None):
    '''
    runs the command:
        `tesseract_cmd` `input_filename` `output_filename_base`

    returns the exit status of tesseract, as well as tesseract's stderr output
    '''
    command = [tesseract_cmd, input_filename, output_filename_base]

    if lang is not None:
        command += ['-l', lang]

    if boxes:
        command += ['batch.nochop', 'makebox']

    if config:
        command += shlex.split(config)

    proc = subprocess.Popen(command,
            stderr=subprocess.PIPE)
    return (proc.wait(), proc.stderr.read())

引用来源的另一部分:

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'

更改tesseract路径的快速方法是:

import pytesseract
pytesseract.tesseract_cmd = "/absolute/path/to/tesseract"  # this should be done only once 
pytesseract.image_to_string(img)

答案 1 :(得分:1)

您可以尝试使用以下python库:https://github.com/prabhakar267/ocr-convert-image-to-text

如软件包自述文件所述,用法非常简单。

usage: python main.py [-h] input_dir [output_dir]

positional arguments:
  input_dir
  output_dir

optional arguments:
  -h, --help  show this help message and exit

答案 2 :(得分:0)

您还需要下载tesseract OCR设置。使用此链接下载设置:http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.01.exe

然后,在代码中包含此行以使用tesseract可执行文件: pytesseract.pytesseract.tesseract_cmd ='C:\ Program Files(x86)\ Tesseract-OCR \ tesseract'

这是安装tesseract的默认位置。

就是这样。我也按照这些步骤在我的最后运行代码。

希望这会有所帮助。

答案 3 :(得分:0)

请安装以下软件包从pnf / jpeg图片中提取文本

pip install pytesseract

pip install Pillow 

使用python pytesseract OCR(光学字符识别)是从图像中电子提取文本的过程

PIL的用途广泛,从简单地读取和写入图像文件到科学的图像处理,地理信息系统,遥感等等。

from PIL import Image
from pytesseract import image_to_string 
print(image_to_string(Image.open('/home/ABCD/Downloads/imageABC.png'),lang='eng'))

答案 4 :(得分:-1)

您的“当前”目录不在您想象的位置。

==&GT;您可以指定图像的完整路径,例如: image = Image.open(r'C:\ Users \ hp \ Downloads \ WinPython-64bit-3.5.1.2 \ python-3.5.1.amd64 \ image.png',mode ='r')

相关问题