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 库 code(dot(google(dot(com/archive/p/pytesser/downloads (抱歉,因为 <10 次我不能发布超过 2 个链接(。

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

GYAZO(dot(com/333f8a3333e87895558f26875a8a8487

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

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

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

import sys
print sys.path

如果你在Python中执行这些行,它会告诉你Python将在哪里寻找鸡蛋。皮特瑟图书馆在那里吗?

此外:作为旁注: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

最新更新