首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PIL导入图像和pytesser导入时出错

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

Stack Overflow用户
提问于 2017-08-20 17:50:15
回答 2查看 739关注 0票数 0

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

代码语言:javascript
复制
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。然而,在运行这段代码时,我得到了这个错误。

代码语言:javascript
复制
    from pytesser import *
ImportError: No module named 'pytesser'

我遵循了在code(dot)google(dot)com/archive/p/pytesser/downloads安装OCR here和PyTesser库的说明(对不起,因为<10 rep我不能发布超过2个链接)。

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

gyazo(dot)com/333f8a3333e87895558f26875a8a8487

我之前也得到了一个关于PIL导入图像的错误。我不应该使用PIL,那么有没有其他方法可以在没有PIL的情况下导入Image?要枕头吗?

我的Python版本是3.5.2,我使用的是windows10。

EN

回答 2

Stack Overflow用户

发布于 2017-08-20 23:04:02

我的第一个预感是,您的库安装在Python不知道的位置。

代码语言:javascript
复制
import sys
print sys.path

如果您在Python中执行这些代码行,它将向您显示Python将在何处查找鸡蛋。pytesser库在那里吗?

此外:顺便说一句:pip3 search tesseract将向您展示一些其他的tesseract Python包。所以您可以使用Python包管理器。

票数 0
EN

Stack Overflow用户

发布于 2018-03-18 22:33:44

将代码更改为:

代码语言:javascript
复制
"""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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45780783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档