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

我是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

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

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

我遵循的指示,从这里安装OCR和这里PyTesser库代码(点)谷歌(点)com /档案/ p / pytesser /下载(抱歉,因为<10代表我不能超过2个链接)。

这个(参见下面的gyazo)是我的安装文件截图,其中“pytesser_v0.0.1”是我的pytesser文件夹,在GitHub上找到了“tesseract-master”(可能不相关),“tessinstall”是其中的文件夹我安装了tesseract,最后pyimgr.py是我正在尝试运行的文件。

gyazo(点)的COM / 333f8a3333e87895558f26875a8a8487

我以前也得到关于PIL导入图像的错误。 我不应该使用PIL,那么有没有其他方式可以在没有PIL的情况下导入图像? 也许枕头?

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


请参阅https://codedump.io/share/bnzB430pwdI0/1/installing-pytesser这可能有帮助,(对不起,我不能评论它太低代表)


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

import sys
print sys.path

如果你在Python中执行这些行,它会告诉你Python将在哪里寻找鸡蛋。 pytesser lib在那里?

此外:请注意: pip3 search tesseract会向您展示其他一些tesseract Python软件包。 所以你可以使用Python包管理器。


将代码更改为:

"""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
链接地址: http://www.djcxy.com/p/96745.html

上一篇: Error with PIL import Image, and pytesser import

下一篇: How to convert a PDF image or an image to text using Tesseract and/or Poppler?