我试图用Python从多个图像中提取文本

发布于 2025-02-12 04:23:03 字数 1685 浏览 5 评论 0原文

这是我的代码

from PIL import Image
from pytesseract import pytesseract
import os

#Define path to tessaract.exe
path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

#Define path to images folder
path_to_images = r'C:\Users\vIBRancy\Desktop\quantic_trust _file\images'

#Point tessaract_cmd to tessaract.exe
pytesseract.tesseract_cmd = path_to_tesseract

#Get the file names in the directory
for root, dirs, file_names in os.walk(path_to_images):
    #Iterate over each file name in the folder
    for file_name in file_names:
        #Open image with PIL
        img = Image.open(path_to_images + file_name)

        #Extract text from image
        text = pytesseract.image_to_string(img, lang = 'eng')
        with open('quantictrustfiles.txt','w') as file:     
      
            file.write(text)
            print(text)

,这是终端的响应:

PS C:\Users\vIBRancy\Desktop\quantic_trust _file> & C:/Users/vIBRancy/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/vIBRancy/Desktop/quantic_trust _file/quantict_trust_file.py"
Traceback (most recent call last):
  File "c:\Users\vIBRancy\Desktop\quantic_trust _file\quantict_trust_file.py", line 19, in <module>
    img = Image.open(path_to_images + file_name)
  File "C:\Users\vIBRancy\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 3068, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\vIBRancy\\Desktop\\quantic_trust _file\\imagesIMG_0096.jpeg'

我不知道该怎么办。任何人都可以提供帮助。

This is my code

from PIL import Image
from pytesseract import pytesseract
import os

#Define path to tessaract.exe
path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

#Define path to images folder
path_to_images = r'C:\Users\vIBRancy\Desktop\quantic_trust _file\images'

#Point tessaract_cmd to tessaract.exe
pytesseract.tesseract_cmd = path_to_tesseract

#Get the file names in the directory
for root, dirs, file_names in os.walk(path_to_images):
    #Iterate over each file name in the folder
    for file_name in file_names:
        #Open image with PIL
        img = Image.open(path_to_images + file_name)

        #Extract text from image
        text = pytesseract.image_to_string(img, lang = 'eng')
        with open('quantictrustfiles.txt','w') as file:     
      
            file.write(text)
            print(text)

Here is the response from the terminal:

PS C:\Users\vIBRancy\Desktop\quantic_trust _file> & C:/Users/vIBRancy/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/vIBRancy/Desktop/quantic_trust _file/quantict_trust_file.py"
Traceback (most recent call last):
  File "c:\Users\vIBRancy\Desktop\quantic_trust _file\quantict_trust_file.py", line 19, in <module>
    img = Image.open(path_to_images + file_name)
  File "C:\Users\vIBRancy\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 3068, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\vIBRancy\\Desktop\\quantic_trust _file\\imagesIMG_0096.jpeg'

I don't know what to do. Anyone to help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文