Key error: "PNG" while using pytesseract python

Viewed 56

Even though I followed the question KeyError: 'PNG' while using pytesseract.image_to_data, I couldn't resolve the problem.

  1. I installed the tesseract in google colab.

    !sudo apt-get install tesseract-ocr
    
  2. then installed pytesseract=0.3.9

    !pip install pytesseract==0.3.9
    
  3. this is my code:

     import cv2
     from pytesseract import Output
     from PIL import Image
     import pytesseract
    
     image = cv2.imread('0003.jpg')
     rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
     results = pytesseract.image_to_data(rgb, output_type=Output.DICT)
    

but I get the error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-25-21a4d6775e8a> in <module>
      9 image = cv2.imread('/content/sample_data/0003.jpg')
     10 rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
---> 11 results = pytesseract.image_to_data(rgb, output_type=Output.DICT)
     12 for i in range(0, len(results["text"])):
     13     x = results["left"][i]

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'
1 Answers

I replicate your situation on my system(google colab) and it works perfectly.

Apart from that, your code is perfectly fine. no bug at all.

I think there is a version issue. You try this version of cv2

cv2 version: 4.6.0

Make sure the image you fetch it should be image.jpg

or

You can upload image and use image path instead of image name.

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

 image = cv2.imread('/content/0001.jpg') # here i use path instead of image name
 rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 results = pytesseract.image_to_data(rgb, output_type=Output.DICT)
 print(results.keys())
Related