在PIL中打开图像文件时出错

时间:2012-03-06 16:09:05

标签: python python-imaging-library

我正在尝试执行以下代码

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'

有人可以解释我在这里做错了什么。

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

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

6 个答案:

答案 0 :(得分:4)

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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="something">
  <i class="fa fa-refresh"></i>
  Some button text
</button>

答案 1 :(得分:3)

异常非常明确:文件不存在,或者您没有足够的权限来访问它。如果不是这种情况,请提供证据(例如,带有输出的相关dir命令,以同一用户身份运行)。

答案 2 :(得分:0)

你的影像路径可能?

i="C:\\Documents and Settings\\Administrator\\Desktop\\attachments\\R1PNDTCB.jpg"

试试这个:

import os
os.path.join("C:\\", "Documents and Settings", "Administrator")

你应该得到一个类似于上一行

的字符串

答案 3 :(得分:0)

首先尝试:

os.path.expanduser('~/Desktop/attachments/R1PNDTCB.jpg')

可能是“文档和设置”中的空格导致了这个问题。

编辑:

使用os.path.join,因此它使用正确的目录分隔符。

答案 4 :(得分:0)

只需在代码中添加这两行

即可

导入操作系统

os.chdir('C:\Python27\Lib\site-packages\pytesser')  

之前

from pytesser import *

答案 5 :(得分:0)

如果您使用的是pytesseract,则必须确保已在系统中安装了Tesseract-OCR。之后,您必须在代码中插入tesseract的路径,如下所示

from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract 
OCR/tesseract'

您可以下载Tesseract-OCR表单https://github.com/UB-Mannheim/tesseract/wiki