简体   繁体   中英

OSError: [WinError 193] %1 is not a valid Win32 application Python

i'm trying to setup a simple OCR with python, I'm now doing tests. I use notepad++, Python 3.10.1, PIP, Pillow, pytesseract. This is my code :

import pytesseract
import numpy 
from PIL import Image, ImageEnhance, ImageFilter

pytesseract.pytesseract.tesseract_cmd = 'Lib/site-packages/pytesseract/pytesseract.py'
im = Image.open(r"C:\Users\Leonid\AppData\Local\Programs\Python\Python310\Lib\site-packages\pytesseract\test.jpg") # Ouverture du fichier image

# Filtrage (augmentation du contraste)
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')

# Lancement de la procédure de reconnaissance
text = pytesseract.image_to_string(im)
print(text)

When I execute the code, i get this error :

Traceback (most recent call last):
  File "C:\Users\Leonid\Desktop\Python\test.py", line 15, in <module>
    text = pytesseract.image_to_string(im)
  File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 416, in image_to_string
    return {
  File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 419, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 286, in run_and_get_output
    run_tesseract(**kwargs)
  File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 257, in run_tesseract
    raise e
  File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 254, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())
  File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
OSError: [WinError 193] %1 n’est pas une application Win32 valide

I tried to install numpy cause i've seen on forums that it solves the problem, and import it ("Import numpy" , line 2). But it still don't work.

What can cause this error and how to fix it?

The problem is this line:

pytesseract.pytesseract.tesseract_cmd = 'Lib/site-packages/pytesseract/pytesseract.py'

You need to set pytesseract.pytesseract.tesseract_cmd to the location of the Tesseract executable. You have instead set it to some Python script.

See this question for some examples of what this should be set to.

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