pytesseract错误在剪贴板上阅读截图

时间:2019-11-13 13:04:41

标签: python ocr python-tesseract

我正在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无法读取剪贴板iamge?

1 个答案:

答案 0 :(得分:1)

可以使用numpy库解决此问题。

安装numpy:

@NgModule({
declarations: [ 
   ItemsListComponent
],
entryComponents: [
    ItemsListComponent
  ]
})

然后添加到导入部分:

pip install numpy

然后相应地更改代码:

import numpy as nm