小编Dan*_*nny的帖子

KeyError:使用 pytesseract.image_to_data 时出现“PNG”

我尝试使用 pytesseract function 在图像文件中的文本周围放置方框image_to_data,但在 colab 上遇到以下错误:

    KeyError                                  Traceback (most recent call last)
<ipython-input-10-a92a28892aac> in <module>()
      6 img = cv2.imread("a.jpg")
      7 
----> 8 d = pytesseract.image_to_data(img, output_type=Output.DICT)
      9 print(d.keys())

5 frames
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in save(self, fp, format, **params)
   2121         expand=0,
   2122         center=None,
-> 2123         translate=None,
   2124         fillcolor=None,
   2125     ):

KeyError: 'PNG'
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码是:

import cv2
import pytesseract
from pytesseract import Output
from PIL import Image

img = cv2.imread("a.jpg")

d = pytesseract.image_to_data(img, output_type=Output.DICT)
print(d.keys())
Run Code Online (Sandbox Code Playgroud)

考虑到可能image_to_data只能使用 PNG 而不能使用 jpeg (这很奇怪),我添加了几行来将 jpeg …

python ocr python-tesseract google-colaboratory

2
推荐指数
1
解决办法
6436
查看次数