Search code examples
pythonopencvpython-tesseract

Is it possible to pytesseract a bytes image?


I am trying to crop an image with cv2 (converting it to a bytes file and therefore not needing to save it)and afterwards perform pytesseract.

This way i won't need to save the image twice during the process.

  1. First when i create the image
  2. When cropping the image

Process...

## CROPPING THE IMAGE REGION

ys, xs = np.nonzero(mask2)
ymin, ymax = ys.min(), ys.max()
xmin, xmax = xs.min(), xs.max()

croped = image[ymin:ymax, xmin:xmax]


pts = np.int32([[xmin, ymin],[xmin,ymax],[xmax,ymax],[xmax,ymin]])
cv2.drawContours(image, [pts], -1, (0,255,0), 1, cv2.LINE_AA)
#OPENCV IMAGE TO BYTES WITHOUT SAVING TO DISK

is_success, im_buf_arr = cv2.imencode(".jpg", croped)
byte_im = im_buf_arr.tobytes()
#PYTESSERACT IMAGE USING A BYTES FILE

Results = pytesseract.image_to_string(byte_im, lang="eng")
print(Results)

Unfortunately i get the error : Unsupported image object

Am i missing something? Is there a way to do this process without needing to save the file when cropping? Any help is highly appreciated.


Solution

  • from PIL import Image
    
    img_tesseract = Image.fromarray(croped)
    Results = pytesseract.image_to_string(img_tesseract, lang="eng")