Python pytesseract.image_to_string() Examples

The following are 30 code examples of pytesseract.image_to_string(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pytesseract , or try the search function .
Example #1
Source File: salts_solution.py    From esoteric-python-challenges with GNU General Public License v3.0 8 votes vote down vote up
def deobfuscator(dict_of_dicts):
    #====Work backwards====
    #Build graph from dict_of_dicts:
    graph_from_dict = nx.DiGraph(dict_of_dicts)

    #Get adjacency matrix of graph
    graph_array = nx.to_numpy_array(graph_from_dict)

    #Change 1's to 255's to save as an image
    graph_array[graph_array == 1] = 255
    image_from_array = Image.fromarray(graph_array).convert("L")
    #We can send the array directly to OCR, but I like to see the image.
    image_from_array.save("obfuscated.png")

    #Run OCR on our image
    return pytesseract.image_to_string("obfuscated.png") 
Example #2
Source File: proxy.py    From get_jobs with MIT License 7 votes vote down vote up
def ocr_get_port(self, data):
        """
        用ocr提取图片中的端口
        :param data: 返回的图片二进制流结果
        :return:
        """

        f = open('port.png', 'wb')
        f.write(data)
        f.close()

        pytesseract.pytesseract.tesseract_cmd = 'C://Program Files//Tesseract-OCR//tesseract.exe'
        port = pytesseract.image_to_string(Image.open('port.png'),
                                           config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')

        # 删除图片
        os.remove('port.png')

        return port 
Example #3
Source File: util.py    From FGO-Automata with MIT License 7 votes vote down vote up
def get_battle_id(img_path: str):
    img = Image.open(img_path)
    region = img.crop((1286, 15, 1378, 62))
    THRESHOLD = 200
    BINARY_TABLE = [0 if i < THRESHOLD else 1 for i in range(256)]
    text = image_to_string(
        region.convert('L').point(BINARY_TABLE, '1'), config='--psm 7 --oem 3 -c tessedit_char_whitelist=/1234')
    print(text)
    try:
        x = int(text[0])
    except IndexError:
        print("Failed to recognize battle id.")
        return 0
    except ValueError:
        print("Failed to recognize battle id.")
        return 0
    else:
        return x 
Example #4
Source File: KnMoney.py    From KnowledgeMoney with MIT License 7 votes vote down vote up
def getImgFromScreenCapture(ques, ans_one, ans_two, ans_thr):
    question = os.system("screencapture -R {} ./question_screenshot.png".format(ques))
    answer_one = os.system("screencapture -R {} ./answers_one.png".format(ans_one))
    answer_two = os.system("screencapture -R {} ./answers_two.png".format(ans_two))
    answer_thr = os.system("screencapture -R {} ./answers_thr.png".format(ans_thr))

    question_img = Image.open("./question_screenshot.png")
    answer_one_img = Image.open("./answers_one.png")
    answer_two_img = Image.open("./answers_two.png")
    answer_thr_img = Image.open("./answers_thr.png")

    question_enh = getImageFromImageEnhanceForQuestion(question_img)
    ans_one_enh  = getImageFromImageEnhance(answer_one_img)
    ans_two_enh  = getImageFromImageEnhance(answer_two_img)
    ans_thr_enh  = getImageFromImageEnhance(answer_thr_img)

    #使用简体中文解析图片
    print('OCR  ' + datetime.datetime.now().strftime('%H:%M:%S'))
    question_text = pytesseract.image_to_string(question_enh, lang='chi_sim')
    question = question_text
    answers = ['','','']
    return question, answers 
Example #5
Source File: helpers.py    From OdooQuant with GNU General Public License v3.0 6 votes vote down vote up
def detect_gf_result(image_path):
    from PIL import ImageFilter, Image
    import pytesseract
    img = Image.open(image_path)
    for x in range(img.width):
        for y in range(img.height):
            if img.getpixel((x, y)) < (100, 100, 100):
                img.putpixel((x, y), (256, 256, 256))
    gray = img.convert('L')
    two = gray.point(lambda x: 0 if 68 < x < 90 else 256)
    min_res = two.filter(ImageFilter.MinFilter)
    med_res = min_res.filter(ImageFilter.MedianFilter)
    for _ in range(2):
        med_res = med_res.filter(ImageFilter.MedianFilter)
    res = pytesseract.image_to_string(med_res, config='-psm 6')
    return res.replace(' ', '') 
Example #6
Source File: idcardocr.py    From idcardocr with GNU General Public License v3.0 6 votes vote down vote up
def get_name(img):
        #    cv2.imshow("method3", img)
        #    cv2.waitKey()
        print('name')
        _, _, red = cv2.split(img) #split 会自动将UMat转换回Mat
        red = cv2.UMat(red)
        red = hist_equal(red)
        red = cv2.adaptiveThreshold(red, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 151, 50)
        #    red = cv2.medianBlur(red, 3)
        red = img_resize(red, 150)
        img = img_resize(img, 150)
        # showimg(red)
        # cv2.imwrite('name.png', red)
        #    img2 = Image.open('address.png')
        # img = Image.fromarray(cv2.UMat.get(red).astype('uint8'))
        #return get_result_vary_length(red, 'chi_sim', img, '-psm 7')
        return get_result_vary_length(red, 'chi_sim', img, '--psm 7')
        # return punc_filter(pytesseract.image_to_string(img, lang='chi_sim', config='-psm 13').replace(" ","")) 
Example #7
Source File: cvmgr.py    From sia-cog with MIT License 6 votes vote down vote up
def extracttext(imgpath, preprocess):
    if imgpath.startswith('http://') or imgpath.startswith('https://') or imgpath.startswith('ftp://'):
        image = url_to_image(imgpath)
    else:
        image = cv2.imread(imgpath)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    if preprocess == "thresh":
        gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    elif preprocess == "blur":
        gray = cv2.medianBlur(gray, 3)

    filename = "{}.png".format(os.getpid())
    cv2.imwrite(filename, gray)
    text = pytesseract.image_to_string(Image.open(filename))

    os.remove(filename)
    return {"text": text} 
Example #8
Source File: text_detection.py    From text-detection with GNU General Public License v3.0 6 votes vote down vote up
def full_OCR(self):
        bounded = self.img.copy()
        res = np.zeros_like(self.gray_img)

        string = image_to_string(Image.open(self.image_file))
        if string == u'':
            return bounded, res

        boxes = image_to_boxes(Image.open(self.image_file))
        boxes = [map(int, i) for i in [b.split(" ")[1:-1] for b in boxes.split("\n")]]

        for box in boxes:
            b = (int(box[0]), int(self.h - box[1]), int(box[2]), int(self.h - box[3]))
            cv2.rectangle(bounded, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 2)
            cv2.rectangle(res, (b[0], b[1]), (b[2], b[3]), 255, -1)

        return bounded, res 
Example #9
Source File: yjbTrader.py    From vxTrader with MIT License 6 votes vote down vote up
def vcode(self):

        r = self._session.get(
            'https://jy.yongjinbao.com.cn/winner_gj/gjzq/user/extraCode.jsp',
            params={'randomStamp': random.random()}
        )
        r.raise_for_status()

        # 通过内存保存数据
        img_buffer = BytesIO(r.content)
        img = Image.open(img_buffer)
        code = pytesseract.image_to_string(img)
        img.close()
        img_buffer.close()

        if self.code_rule.findall(code) == []:
            raise VerifyCodeError('Wrong verify code: %s' % code)
        else:
            logger.debug('Verify Code is: %s' % code)
            return code 
Example #10
Source File: captcha.py    From easytrader with MIT License 6 votes vote down vote up
def captcha_recognize(img_path):
    import pytesseract

    im = Image.open(img_path).convert("L")
    # 1. threshold the image
    threshold = 200
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)

    out = im.point(table, "1")
    # 2. recognize with tesseract
    num = pytesseract.image_to_string(out)
    return num 
Example #11
Source File: twitter.py    From mcafee2cash with MIT License 6 votes vote down vote up
def handle_tweet(self, tweet_json):
		screen_name = tweet_json["user"]["screen_name"]
		id = tweet_json["id_str"]
		text = tweet_json["text"].replace("\\", "")

		# Get media if present
		try:
			urls = [x["media_url"].replace("\\", "") for x in tweet_json["entities"]["media"] if x["type"] == "photo"]
			for url in urls:
				response = requests.get(url)
				img = Image.open(io.BytesIO(response.content))
				# Extract text from image
				img_text = pytesseract.image_to_string(img)
				text += f' . {img_text}'
		except KeyError:
			pass

		link = f'https://twitter.com/{screen_name}/status/{id}'

		try:
			self.tweet_callback(text, screen_name, link)
		except:
			pass 
Example #12
Source File: pyinrail.py    From pyinrail with MIT License 6 votes vote down vote up
def create_session(self):
        """
        create a session by solving captcha challenge
        """
        self.session['timestamp'] = int(time.time() * 1000)
        url = "http://www.indianrail.gov.in/enquiry/captchaDraw.png?{}".format(self.session['timestamp'])
        r = requests.get(url)
        self.session['cookies'] = r.cookies
        try:
            f = BytesIO(r.content)
        except OSError:
            return None
        im = Image.open(f)
        text = pytesseract.image_to_string(im, lang = 'eng')
        try:
            self.session['captcha'] = eval(text.split("=")[0])
        except:
            self.create_session() 
Example #13
Source File: autocaptcha.py    From sjtu-automata with GNU General Public License v3.0 6 votes vote down vote up
def autocaptcha(path):
    """Auto identify captcha in path.

    Use pytesseract to identify captcha.

    Args:
        path: string, image path.

    Returns:
        string, OCR identified code.
    """
    im = Image.open(path)

    im = im.convert('L')
    im = ImageEnhance.Contrast(im)
    im = im.enhance(3)
    img2 = Image.new('RGB', (150, 60), (255, 255, 255))
    img2.paste(im.copy(), (25, 10))

    # TODO: add auto environment detect
    return pytesseract.image_to_string(img2) 
Example #14
Source File: domainhunter.py    From domainhunter with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def solveCaptcha(url,session):  
    # Downloads CAPTCHA image and saves to current directory for OCR with tesseract
    # Returns CAPTCHA string or False if error occured
    
    jpeg = 'captcha.jpg'
    
    try:
        response = session.get(url=url,headers=headers,verify=False, stream=True,proxies=proxies)
        if response.status_code == 200:
            with open(jpeg, 'wb') as f:
                response.raw.decode_content = True
                shutil.copyfileobj(response.raw, f)
        else:
            print('[-] Error downloading CAPTCHA file!')
            return False

        # Perform basic OCR without additional image enhancement
        text = pytesseract.image_to_string(Image.open(jpeg))
        text = text.replace(" ", "")
        
        # Remove CAPTCHA file
        try:
            os.remove(jpeg)
        except OSError:
            pass

        return text

    except Exception as e:
        print("[-] Error solving CAPTCHA - {0}".format(e))
        
        return False 
Example #15
Source File: helpers.py    From DevilYuan with MIT License 5 votes vote down vote up
def detect_yh_client_result(image_path):
    """封装了tesseract的识别,部署在阿里云上,服务端源码地址为: https://github.com/shidenggui/yh_verify_code_docker"""
    
    image = Image.open(image_path)
    code = pytesseract.image_to_string(image, config='-psm 7')
    return code.replace(' ', '') 
Example #16
Source File: extract_subtitles.py    From extract-subtitles with MIT License 5 votes vote down vote up
def ocr_im(name):
    global ADJUST_MODE
    im=Image.open(dir + name)
    inverted_im=PIL.ImageOps.invert(im)
    #inverted_im.show()
    croped_im=inverted_im.crop((x,y,x+w,y+h))
    if ADJUST_MODE and USE_CROP:
        croped_im.show()
        ADJUST_MODE = False
    text=pytesseract.image_to_string(croped_im if USE_CROP else inverted_im, LANG)
    return text
    

#Print infos 
Example #17
Source File: analyze.py    From hq-answer-assist with Apache License 2.0 5 votes vote down vote up
def tesseract_orc(image):
    text = pytesseract.image_to_string(image, lang='chi_sim')
    print('识别的文字是: {}'.format(text))
    return get_question(text)


# 使用百度ocr识别 
Example #18
Source File: ocr.py    From spntaBot with GNU General Public License v3.0 5 votes vote down vote up
def run(message, matches, chat_id, step):
        if 'reply_to_message' in message:
            reply = message['reply_to_message']
            if 'photo' in reply:
                await download(reply['photo'][1]['file_id'], 'tmp/ocr{}.jpg'.format(message['from']['id']))
                text = pytesseract.image_to_string(Image.open('tmp/ocr{}.jpg'.format(message['from']['id'])))
                os.remove('tmp/ocr{}.jpg'.format(message['from']['id']))
                return [Message(chat_id).set_text(text)] 
Example #19
Source File: example-2.py    From python-examples with MIT License 5 votes vote down vote up
def get_text(image, region):
    return pytesseract.image_to_string(image.crop(region)) 
Example #20
Source File: idcardocr.py    From idmatch with MIT License 5 votes vote down vote up
def recognize_card(idcard):
    result = []
    # TODO: 
    # process_image(original_image, cropped_image)
    # idcard = cv2.imread(cropped_, cv2.COLOR_BGR2GRAY)

    # In some cases resized image gives worse results
    # idcard = resize(idcard, width=720)

    gray = cv2.cvtColor(idcard, cv2.COLOR_BGR2GRAY)
    denoised = cv2.fastNlMeansDenoising(gray, None, 3, 7, 21)
    
    contours, hierarchy = recognize_text(gray)
    mask = np.zeros(gray.shape, np.uint8)

    for index, contour in enumerate(contours):
        [x, y, w, h] = cv2.boundingRect(contour)
        if h < 16 or w < 16:
            continue

        mskRoi = mask[y:y+h, x:x+w]
        cv2.drawContours(mask, [contour], 0, 255, -1) #CV_FILLED
        nz = cv2.countNonZero(mskRoi)
        ratio = (float)(nz) / (float)(h*w)
        
        # got this value from left heel
        if ratio > 0.55 and ratio < 0.9:
            roi = denoised[y:y+h, x:x+w] 
            text = pytesseract.image_to_string(Image.fromarray(roi), lang="kir+eng", config="-psm 7")
            if text:                
                item = {'x': x, 'y': y, 'w': w, 'h': h, 'text': text}
                result.append(item)
                cv2.rectangle(idcard, (x, y), (x + w, y + h), (255, 0, 255), 2)
    # need to restore settings
    hash_object = hashlib.sha256(idcard)
    hex_dig = hash_object.hexdigest()
    cv2.imwrite("/webapp/web/static/"+hex_dig+".jpeg", idcard)
    return "static/"+hex_dig+".jpeg", result 
Example #21
Source File: captcha.py    From easytrader with MIT License 5 votes vote down vote up
def invoke_tesseract_to_recognize(img):
    import pytesseract

    try:
        res = pytesseract.image_to_string(img)
    except FileNotFoundError:
        raise Exception(
            "tesseract 未安装,请至 https://github.com/tesseract-ocr/tesseract/wiki 查看安装教程"
        )
    valid_chars = re.findall("[0-9a-z]", res, re.IGNORECASE)
    return "".join(valid_chars) 
Example #22
Source File: idcardocr.py    From idcardocr with GNU General Public License v3.0 5 votes vote down vote up
def get_address(img):
        #_, _, red = cv2.split(img)
        #red = cv2.medianBlur(red, 3)
        print('address')
        _, _, red = cv2.split(img)
        red = cv2.UMat(red)
        red = hist_equal(red)
        red = cv2.adaptiveThreshold(red, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 151, 50)
        red = img_resize(red, 300)
        #img = img_resize(img, 300)
        #cv2.imwrite('address_red.png', red)
        img = Image.fromarray(cv2.UMat.get(red).astype('uint8'))
        #return punc_filter(get_result_vary_length(red,'chi_sim', img, '-psm 6'))
        return punc_filter(get_result_vary_length(red, 'chi_sim', img, '--psm 6'))
        #return punc_filter(pytesseract.image_to_string(img, lang='chi_sim', config='-psm 3').replace(" ","")) 
Example #23
Source File: verification.py    From quantproject with Apache License 2.0 5 votes vote down vote up
def tesseract(image,lang):
    """
    pytesseract ocr
    """
    return image_to_string(image, lang=lang, config='--psm 7') 
Example #24
Source File: raidnearby.py    From PGSS with GNU General Public License v3.0 5 votes vote down vote up
def detectTime(self, time_binary):
#        img_gray = cv2.cvtColor(time_img, cv2.COLOR_BGR2GRAY)
#        ret, thresh1 = cv2.threshold(img_gray, 230, 255, cv2.THRESH_BINARY_INV)
        final_img = np.zeros((time_binary.shape[0], int(time_binary.shape[1] * 0.25)), np.uint8)
        right_img = np.zeros((time_binary.shape[0], int(time_binary.shape[1] * 0.15)), np.uint8)
        separate_img = np.zeros((time_binary.shape[0], int(time_binary.shape[1] * 0.1)), np.uint8)
        profile = []
        letter_start = []
        letter_end = []
        count = 0
        valley_threshold = 256
        # get letters separation pixels
        for i in range(time_binary.shape[1]):
            sum_vertical = sum(time_binary[:, i])
            profile.append(sum_vertical)
            if len(letter_start) == len(letter_end):
                if sum_vertical > valley_threshold:
                    letter_start.append(i)
            else:
                if sum_vertical <= valley_threshold:
                    letter_end.append(i)
                    count = count + 1
        # Add blank(black) space between letters
        for i in range(count):
            final_img = cv2.hconcat([final_img, time_binary[0:time_binary.shape[0], letter_start[i]:letter_end[i]]])
            final_img = cv2.hconcat([final_img, separate_img])
        final_img = cv2.hconcat([final_img, right_img])
        kernel = np.ones((2, 2), np.uint8)
        final_img = cv2.dilate(final_img, kernel, iterations=1)
        cv2.imwrite(self.timefile, final_img)
        text = pytesseract.image_to_string(Image.open(self.timefile),
                                           config='-c tessedit_char_whitelist=1234567890:~-AMP -psm 7')
        return text 
Example #25
Source File: codeserver.py    From inshack-2018 with GNU General Public License v3.0 5 votes vote down vote up
def equation():
    if recaptcha.verify():
        if 'file' not in request.files:
            return render_template('result.html', result = "No file uploaded")
        file = request.files['file']
        print(file)
        if file and file.filename == '':
            return render_template('result.html', result = "No correct file uploaded")
        if file:
            input_text = pytesseract.image_to_string(Image.open(BytesIO(file.read())))
            print(input_text)
            formated_text = "=".join(input_text.split("\n"))
            formated_text = formated_text.replace("=","==")
            formated_text = sub('===+','==',formated_text)
            formated_text = formated_text.replace(" ","")
            print(formated_text)
            if any(i not in 'abcdefghijklmnopqrstuvwxyz0123456789()[]=+-*' for i in formated_text):
                return render_template('result.html', result = "Some features are still in beta !")
            if formated_text.count('(') > 1 or formated_text.count(')') > 1 or formated_text.count('[') > 1 or formated_text.count(']') > 1 :
                return render_template('result.html', result = "We can not solve complex equations for now !")
            if any(i in formated_text for i in ["import","exec","compile","tesseract","chr","os","write","sleep"]):
                return render_template('result.html', result = "We can not understand your equation !")
            if len(formated_text) > 15:
                return render_template('result.html', result = "We can not solve complex equations for now !")
            try:
                if "==" in formated_text:
                    parts = formated_text.split("==",maxsplit=2)
                    pa_1 = int(eval(parts[0]))
                    pa_2 = int(eval(parts[1]))
                    if pa_1 == pa_2:
                        return render_template('result.html', result = "Wow, it works !")
                    else:
                        return render_template('result.html', result = "Sorry but it seems that %d is not equal to %d"%(pa_1,pa_2))
                else:
                    return render_template('result.html', result = "Please import a valid equation !")
            except (KeyboardInterrupt, SystemExit):
                raise
            except:
                return render_template('result.html', result = "Something went wrong...") 
Example #26
Source File: KnMoney.py    From KnowledgeMoney with MIT License 5 votes vote down vote up
def getImgFromScreenCaptureAgain(ques, ans_one, ans_two, ans_thr):
    question = os.system("screencapture -R \" {} \" ./question_screenshot.png".format(ques))
    answer_one = os.system("screencapture -R \"{}\" ./answers_one.png".format(ans_one))
    answer_two = os.system("screencapture -R \"{}\" ./answers_two.png".format(ans_two))
    answer_thr = os.system("screencapture -R \"{}\" ./answers_thr.png".format(ans_thr))

    question_img = Image.open("./question_screenshot.png")
    answer_one_img = Image.open("./answers_one.png")
    answer_two_img = Image.open("./answers_two.png")
    answer_thr_img = Image.open("./answers_thr.png")

    question_enh = getImageFromImageEnhanceForQuestion(question_img)
    ans_one_enh  = getImageFromImageEnhance(answer_one_img)
    ans_two_enh  = getImageFromImageEnhance(answer_two_img)
    ans_thr_enh  = getImageFromImageEnhance(answer_thr_img)

    #使用简体中文解析图片
    # print('OCR  ' + datetime.datetime.now().strftime('%H:%M:%S'))
    question_text = pytesseract.image_to_string(question_enh, lang='chi_sim')
    # print(question_text)
    ans_one_text = pytesseract.image_to_string(ans_one_enh, lang='chi_sim')
    # print(ans_one_text)
    ans_two_text = pytesseract.image_to_string(ans_two_enh, lang='chi_sim')
    # print(ans_two_text)
    ans_thr_text = pytesseract.image_to_string(ans_thr_enh, lang='chi_sim')
    # print(ans_thr_text)
    question = question_text
    answers = [ans_one_text, ans_two_text, ans_thr_text]
    # print(answers)
    return question, answers 
Example #27
Source File: raidnearby.py    From PGSS with GNU General Public License v3.0 5 votes vote down vote up
def detectRaidBossTimer(self, time_img, scale):
        text = ''
        if int(time_img.mean()) > 240:
            return text
        time_img = cv2.resize(time_img, None, fx=1.0/scale, fy=1.0/scale, interpolation=cv2.INTER_CUBIC)
        cv2.imwrite(self.timefile,time_img)
        text = pytesseract.image_to_string(Image.open(self.timefile),config='-c tessedit_char_whitelist=1234567890: -psm 7')
        return text 
Example #28
Source File: get_time.py    From edusense with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_timestamp(frame):

      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      cropped_image=gray[80:150,3000:3800]
      cropped_image=cv2.resize(cropped_image,(800,100))
      binary = cv2.adaptiveThreshold(cropped_image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,60)
      text = pytesseract.image_to_string(binary,config='--psm 13  -c tessedit_char_whitelist=:-0123456789APM" " ')
      return text; 
Example #29
Source File: get_time.py    From edusense with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_timestamp(frame):

      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      cropped_image=gray[80:150,3000:3800]
      cropped_image=cv2.resize(cropped_image,(800,100))
      binary = cv2.adaptiveThreshold(cropped_image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,60)
      text = pytesseract.image_to_string(binary,config='--psm 13  -c tessedit_char_whitelist=:-0123456789APM" " ')
      return text; 
Example #30
Source File: ocr_main.py    From TesseractOCR with MIT License 5 votes vote down vote up
def main():
    # Get File Name from Command Line
    path = input("Enter the file path : ").strip()
    
    # Load the required image
    image = cv2.imread(path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    preprocess = False
    temp = input(
        "Do you want to pre-process the image ?\nThreshold : 1\nGrey : 2\nNone : 0\nEnter your choice : ").strip()

    # If user enters 1, Process Threshold. Else if user enters 2, Process Median Blur. Else, do nothing
    if temp == "1":
        gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    elif temp == "2":
        gray = cv2.medianBlur(gray, 3)

    # Store grayscale image as a temporary file to apply OCR
    filename = "{}.png".format("temp")
    cv2.imwrite(filename, gray)

    # Load the image as a PIL/Pillow image, apply OCR, and then delete the temporary file
    text = pytesseract.image_to_string(Image.open(filename))

    print("OCR Text is " + text)