from PIL import Image
from io import BytesIO
import base64
import uuid
import os
import cv2
import pytesseract

class ImageToText():

    def extractText(self, image_path):
        image = cv2.imread(image_path)
        os.environ['TESSDATA_PREFIX'] = '/usr/share/tesseract-ocr/5/tessdata'
        texto = pytesseract.image_to_string(image)
        os.remove(image_path)
        return texto
        


    def init(self, data):
        image_bytes = data['data'].split(',')[1].encode('utf-8')
        image = Image.open(BytesIO(base64.b64decode(image_bytes)))
        random_filename = str(uuid.uuid4()) + '.png'
        image.save('./tmp/' + random_filename)

        image_path = './tmp/' + random_filename
        return image_path