I have written a function that reads the clipboard image whence captured as screenshot and then passes that captured image data to OCR engine. I am struggling with the passing of data. The code is given below.
from tkinter import messagebox
from PIL import Image
import pytesseract
import numpy as np
img = Image.grabclipboard() #this part needs modification as grabclipboard works in Windows and not in Linux.
if isinstance(img, Image.Image):
rd = pytesseract.image_to_string(np.array(img), lang='eng') \
.replace('-\n', '').replace('\n', ' ').encode("ascii", 'ignore')
print(rd)
else:
messagebox.showinfo(title="TASK DONE", message="No Image on Clipboard Found.")
If you see above code then line if isinstance(img, Image.Image):
fails to capture the clipboard data and the loop jumps to else messagebox
.
I want that clipboard content should be read into the OCR engine used here.
I have found that pyscreenshot
can be of use in this code. Here is an example of the code.
import pyscreenshot as ImageGrab
def grabocr(self, x, y, w, h):
im = ImageGrab.grab(bbox=(x, y, x+w, y+h))
pix = im.load()
for x in range(im.size[0]):
for y in range(im.size[1]):
if pix[x, y] != (254, 254, 254):
pix[x, y] = 0;
return pytesseract.image_to_string(im)
Can somebody improve my code based on this one? All I need is to capture the screenshot and send it to my OCR steps. All successive discussions on Gtk may kindly be ignored.