import os import tkinter as tk import pytesseract import cv2 import pyperclip import numpy as np from pytesseract import Output # import argparse # import pyautogui as gui # from threading import Thread, Event # from PIL import Image, ImageOps, ImageEnhance # from time import sleep # thread = Thread(target=gui.prompt) def thin_font(image): image = cv2.bitwise_not(image) kernel = np.ones((2, 2), np.uint8) image = cv2.erode(image, kernel, iterations=1) image = cv2.bitwise_not(image) return (image) def thick_font(image): image = cv2.bitwise_not(image) kernel = np.ones((2, 2), np.uint8) image = cv2.dilate(image, kernel, iterations=1) image = cv2.bitwise_not(image) return (image) def noise_removal(image): kernel = np.ones((1, 1), np.uint8) image = cv2.dilate(image, kernel, iterations=1) kernel = np.ones((1, 1), np.uint8) image = cv2.erode(image, kernel, iterations=1) image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) image = cv2.medianBlur(image, 3) return (image) def process_tess(file): image = cv2.imread(file) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # gray = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # gray = thin_font(gray) # _thresh, im_bw = cv2.threshold(gray, 210, 230, cv2.THRESH_BINARY) results = pytesseract.image_to_data(gray, output_type=Output.DICT) text = '\n'.join(results['text']) return text def main_tess(ext='.png'): im_files = [f for f in os.listdir() if f.endswith(ext)] im_files.sort(key=lambda x: os.stat(os.path.join(x)).st_mtime, reverse=True) text = process_tess(im_files[0]) pyperclip.copy(text) root = tk.Tk() T = tk.Text(root, height=10, width=50) T.pack() T.insert(tk.END, text) tk.mainloop() # os.remove(im_files[0]) # gui.prompt(title='ocr', default=text) # parser = argparse.ArgumentParser() # parser.add_argument('-file', type=str, help='name of the scanned file to be converted', # default=img_files[0]) # parser.add_argument('-p', '--preprocess', type=str, default='thresh', # help='type of preprocessing to be done') # args = parser.parse_args() # image = cv2.imread(args.file) # rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # if args.preprocess == "thresh": # gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] # elif args.preprocess == "blur": # gray = cv2.medianBlur(gray, 3) # cv2.imwrite('new_image.png', gray) # print('tesseract version {}'.format(pytesseract.get_tesseract_version())) # print('processing', args.file) # results = pytesseract.image_to_data(gray, output_type=Output.DICT) # text = ' '.join(results['text']) # pyperclip.copy(text) # gui.prompt(title='ocr', default=text) # text = pytesseract.image_to_string(gray) if __name__ == '__main__': main_tess()