PIL导入图像和pytesser导入时出错

时间:2017-08-20 09:50:15

标签: python ocr tesseract python-tesseract

我是Python新手。我正在尝试创建一个Python OCR程序,并在线跟踪它的教程。以下是我使用的推荐代码:

from PIL import Image
from pytesser import *

image_file = 'menu.tif'
im = Image.open(image_file)
text = image_to_string(im)
text = image_file_to_string(image_file)
text = image_file_to_string(image_file, graceful_errors=True)
print "=====output=======\n"
print text

找到教程链接here。但是,在运行此代码时出现此错误。

    from pytesser import *
ImportError: No module named 'pytesser'

我按照说明安装了OCR here和PyTesser库代码(点)google(dot)com / archive / p / pytesser / downloads(抱歉因为< 10 rep i can&#39 ; t发布超过2个链接)。

这(请参阅下面的gyazo)是我目前安装文件的截图,其中" pytesser_v0.0.1"是我的pytesser文件夹," tesseract-master"在GitHub上找到(可能不相关),以及" tessinstall"是我安装tesseract的文件夹,最后pyimgr.py是我试图运行的文件。

gyazo(点)的COM / 333f8a3333e87895558f26875a8a8487

我之前也收到有关PIL导入图片的错误。我不应该使用PIL,所以有没有其他方法我可以导入图像没有PIL?也许枕头?

我的Python版本是3.5.2,我使用的是Windows 10。

2 个答案:

答案 0 :(得分:0)

我的第一个预感是你的库安装在Python不知道的地方。

import sys
print sys.path

如果你在Python中执行这些行,它将显示Python将寻找鸡蛋的位置。是pytesser lib吗?

此外:作为旁注: pip3 search tesseract将向您展示其他一些tesseract Python包。所以你可以使用Python包管理器。

答案 1 :(得分:0)

将代码更改为:

"""OCR in Python using the Tesseract engine from Google
http://code.google.com/p/pytesser/
by Michael J.T. O'Kelly
V 0.0.1, 3/10/07"""

import PIL.Image
import subprocess

import util
import errors

tesseract_exe_name = 'tesseract' # Name of executable to be called at command line
scratch_image_name = "temp.bmp" # This file must be .bmp or other Tesseract-compatible format
scratch_text_name_root = "temp" # Leave out the .txt extension
cleanup_scratch_flag = True  # Temporary files cleaned up after OCR operation

def call_tesseract(input_filename, output_filename):
    """Calls external tesseract.exe on input file (restrictions on types),
    outputting output_filename+'txt'"""
    args = [tesseract_exe_name, input_filename, output_filename]
    proc = subprocess.Popen(args)
    retcode = proc.wait()
    if retcode!=0:
        errors.check_for_errors()

def image_to_string(im, cleanup = cleanup_scratch_flag):
    """Converts im to file, applies tesseract, and fetches resulting text.
    If cleanup=True, delete scratch files after operation."""
    try:
        util.image_to_scratch(im, scratch_image_name)
        call_tesseract(scratch_image_name, scratch_text_name_root)
        text = util.retrieve_text(scratch_text_name_root)
    finally:
        if cleanup:
            util.perform_cleanup(scratch_image_name, scratch_text_name_root)
    return text

def image_file_to_string(filename, cleanup = cleanup_scratch_flag, graceful_errors=True):
    """Applies tesseract to filename; or, if image is incompatible and graceful_errors=True,
    converts to compatible format and then applies tesseract.  Fetches resulting text.
    If cleanup=True, delete scratch files after operation."""
    try:
        try:
            call_tesseract(filename, scratch_text_name_root)
            text = util.retrieve_text(scratch_text_name_root)
        except errors.Tesser_General_Exception:
            if graceful_errors:
                im = PIL.Image.open(filename)
                text = image_to_string(im, cleanup)
            else:
                raise
    finally:
        if cleanup:
            util.perform_cleanup(scratch_image_name, scratch_text_name_root)
    return text


if __name__=='__main__':
    im = PIL.Image.open('phototest.tif')
    text = image_to_string(im)
    print text
    try:
        text = image_file_to_string('fnord.tif', graceful_errors=False)
    except errors.Tesser_General_Exception, value:
        print "fnord.tif is incompatible filetype.  Try graceful_errors=True"
        print value
    text = image_file_to_string('fnord.tif', graceful_errors=True)
    print "fnord.tif contents:", text
    text = image_file_to_string('fonts_test.png', graceful_errors=True)
    print text
相关问题