Error 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 explain what I'm doing wrong here.

Renamed the image file. Dumped the python file and images to a new folder. Shift the folder to drive E 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 
+6
source share
6 answers

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

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

The exception is quite clear: the file either does not exist, or you do not have sufficient rights to access it. If this is not the case, please provide evidence (for example, the corresponding dir commands with the output, work as the same user).

+3
source

maybe your path to the image?

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

try the following:

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

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

0
source

Try it first:

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

Perhaps this place in the "Documents and Settings" causes this problem.

EDIT:

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

0
source

Just add these two lines to your code

import OS

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

before

 from pytesser import * 
0
source

If you are using pytesseract, you must ensure that you have installed Tesseract-OCR on your system. After that, you should insert the path in tesseract into your code, as shown 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

0
source

Source: https://habr.com/ru/post/910096/


All Articles