Redstain
Redstain

Reputation: 187

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.

Upvotes: 1

Views: 2420

Answers (3)

MSDeen
MSDeen

Reputation: 181

from PIL import Image
import io
def bytes_to_image(image_bytes):
    io_bytes = io.BytesIO(image_bytes)
    return Image.open(io_bytes)
pytesseract.image_to_data(byte_array_image,lang='eng')

Upvotes: 0

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15329

you have croped which is a numpy array.

according to pytesseract examples, you simply do this:

# tesseract needs the right channel order
cropped_rgb = cv2.cvtColor(croped, cv2.COLOR_BGR2RGB)

# give the numpy array directly to pytesseract, no PIL or other acrobatics necessary
Results = pytesseract.image_to_string(cropped_rgb, lang="eng")

Upvotes: 1

Hihikomori
Hihikomori

Reputation: 990

from PIL import Image

img_tesseract = Image.fromarray(croped)
Results = pytesseract.image_to_string(img_tesseract, lang="eng")

Upvotes: 0

Related Questions