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

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

我正在尝试使用 python 设置一个简单的 OCR,我现在正在进行测试。 我使用记事本++、Python 3.10.1、PIP、Pillow、pytesseract。 这是我的代码:

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)

当我执行代码时,出现此错误:

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

我尝试安装 numpy,因为我在论坛上看到它可以解决问题,然后将其导入(“导入 numpy”,第 2 行)。但是还是不行。

什么会导致此错误以及如何修复它?

问题出在这一行:

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

您需要将 pytesseract.pytesseract.tesseract_cmd 设置为 Tesseract 可执行文件的位置。您已将其设置为某个 Python 脚本。

请参阅 this question 以了解应该设置的一些示例。