简体   繁体   中英

Error in opening image file in 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

I get the following error

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.

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

Now the error is as follows:

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

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'

The exception is pretty clear: the file either doesn't exist, or you lack sufficient permissions to access it. If neither is the case, please provide evidence (eg relevant dir commands with output, run as the same user).

Just add these two lines in your code

import OS

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

before

from pytesser import *

If you are using pytesseract, you have to make sure that you have installed Tesseract-OCR in your system. After that you have to insert the path of the tesseract in your code, as below

from PIL import Image
import pytesseract

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

You can download the Tesseract-OCR form https://github.com/UB-Mannheim/tesseract/wiki

your image path maybe?

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

try this:

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

you should get a string similar to the one in the previous line

Try this first:

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

It could be that the space in the 'Documents and Settings' is causing this problem.

EDIT:

Use os.path.join so it uses the correct directory separator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM