在PIL中打开图像文件时出错 [英] Error in opening image file in PIL

查看:375
本文介绍了在PIL中打开图像文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下代码

I am trying to execute the following code

from pytesser import *
import Image

i="C:/Documents and Settings/Administrator/Desktop/attachments/R1PNDTCB.jpg"
print i
im = Image.open(i.strip())
text = image_to_string(im)
print text

我收到以下错误

C:/Documents and Settings/Administrator/Desktop/attachments/R1PNDTCB.jpg
Traceback (most recent call last):
  File "C:\Python27\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 322, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "C:\Python27\Lib\site-packages\Pythonwin\pywin\debugger\__init__.py", line 60, in run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "C:\Python27\Lib\site-packages\Pythonwin\pywin\debugger\debugger.py", line 655, in run
    exec cmd in globals, locals
  File "C:\Documents and Settings\Administrator\Desktop\attachments\ocr.py", line 1, in <module>
    from pytesser import *
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1952, in open
    fp = __builtin__.open(fp, "rb")
IOError: [Errno 2] No such file or directory: 'C:/Documents and Settings/Administrator/Desktop/attachments/R1PNDTCB.jpg'

有人可以在这里解释我做错了吗.

Can someone please explain what I am doing wrong here.

重命名图像文件.将python文件和图像移至新文件夹.将文件夹移至E盘 现在的代码如下:

Renamed the image file.Shifted the python file and the images to a new folder. Shifted the folder to E drive Now the code is as follows:

from pytesser import *
import Image
import os

i=os.path.join("E:\\","ocr","a.jpg")
print i
im = Image.open(i.strip())
text = image_to_string(im)
print text

现在错误如下:

E:\ocr\a.jpg
Traceback (most recent call last):
  File "or.py", line 8, in <module>
    text = image_to_string(im)
  File "C:\Python27\lib\pytesser.py", line 31, in image_to_string
    call_tesseract(scratch_image_name, scratch_text_name_root)
  File "C:\Python27\lib\pytesser.py", line 21, in call_tesseract
    proc = subprocess.Popen(args)
  File "C:\Python27\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

推荐答案

您需要先安装Tesseract.仅仅安装pytesseract是不够的.然后在pytesseract.py中编辑tesseract_cmd变量,以指向tessseract二进制文件.例如,在我的安装中,将其设置为

You need to install Tesseract first. Just installing pytesseract is not enough. Then edit the tesseract_cmd variable in pytesseract.py to point the the tessseract binary. For example, in my installation I set it to

tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'

这篇关于在PIL中打开图像文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆