读取剪贴板上的屏幕截图时出现PyteSeract错误

2024-04-18 15:39:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python代码中使用pytesseract并用于阅读[剪贴板上的屏幕截图]。它给我以下错误

PS D:\Git\NOTEMAN> python nsu63w.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "nsu63w.py", line 197, in clpocr
    rd = pytesseract.image_to_string(img, lang='eng') \
  File "C:\Python\Python38\lib\site-packages\pytesseract\pytesseract.py", line 341, in image_to_string
    return {
  File "C:\Python\Python38\lib\site-packages\pytesseract\pytesseract.py", line 344, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Python\Python38\lib\site-packages\pytesseract\pytesseract.py", line 242, in run_and_get_output
    temp_name, input_filename = save_image(image)
  File "C:\Python\Python38\lib\site-packages\pytesseract\pytesseract.py", line 171, in save_image
    image, extension = prepare(image)
  File "C:\Python\Python38\lib\site-packages\pytesseract\pytesseract.py", line 149, in prepare
    raise TypeError('Unsupported image format/type')
TypeError: Unsupported image format/type

因为我使用的是windows平台。我不确定问题出在哪里。使用的代码如下所示

def clpocr():
    rfi = tb4.get() + " {" + tb5.get() + "}\n"
    b = len(tb6.get())
    if tb6.get() == "Folder Name Here" or b < 2:
        messagebox.showinfo(title="PROCEDURAL ERROR",
                            message="Set FOLDER & NEW_NOTE.")
        return

    if tessloc != 'not_set':
        count = 0
        a = foloc
        if len(a) > 2:
            img = ImageGrab.grabclipboard()
            if isinstance(img, Image.Image):
                rd = pytesseract.image_to_string(img, lang='eng') \
                    .replace('-\n', '').replace('\n', ' ').encode("ascii", 'ignore')
                tb1.insert('insert', rfi)
                tb1.insert('insert', rd)
                tb1.insert('insert', br)
                tb1.clipboard_clear()
                count += 1
            else:
                messagebox.showinfo(title="TASK DONE", message="No Image Files Found.")
        else:
            count -= 1
            messagebox.showinfo(title="PROCEDURAL ERROR",
                                message="Set FOLDER & NEW_NOTE.")

        if count > 0:
            gg = str(count) + " Files Read."
            messagebox.showinfo(title="TASK DONE", message=gg)

    else:
        messagebox.showinfo(title="PROCEDURAL ERROR", message="Set TESSLOC First.")

问题似乎出在img = ImageGrab.grabclipboard()。为什么pytesseract不能读取剪贴板图像


Tags: inpyimageimggetiflibpackages