0

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.

  • Out put is as follows `` – Ambrish Dhaka Jan 14 '20 at 17:31
  • ***``***: This explains, why `if isinstance(img, Image.Image):` evals to `False`. Where did you get the suggestion to use `Gtk.Clipboard.get` from? – stovfl Jan 14 '20 at 17:33
  • well it is a long story. I have developed the same thing in windows platform because there `ImageGrab` works. In Linux there is no such thing. So, It is just a trial thing. Can you suggest and alternate to this. – Ambrish Dhaka Jan 14 '20 at 17:35
  • My result from `.wait_for_image()`: ` – stovfl Jan 14 '20 at 22:06
  • Sorry for the late reply. I have seen your reference to code in some examples. Being an amateur I am unable to get it on my own. I need a little more help by showing this into my code. If you please. The exit problem you have linked to was certainly not my case. – Ambrish Dhaka Jan 16 '20 at 02:52
  • The whole thing is stuck on just one thing. There is no equivalent to ImageGrab from the Pillow library in Linux. Something which works smoothly in windows. I need an alternative to ImageGrab to be used here. – Ambrish Dhaka Jan 16 '20 at 02:56
  • Try [Tutorial - Clipboard](https://python-gtk-3-tutorial.readthedocs.io/en/latest/clipboard.html) – stovfl Jan 16 '20 at 10:15
  • thanks, I have seen this. But it is not for Tkinter. – Ambrish Dhaka Jan 16 '20 at 16:51
  • ***"But it is not for Tkinter"***: Is it working for you as it is? – stovfl Jan 16 '20 at 17:00
  • I never used GTk, I used pillow libraries to handle clipboard. So, of course it is not working. – Ambrish Dhaka Jan 17 '20 at 06:29
  • Read [ImageGrab Module (macOS and Windows only)](https://pillow.readthedocs.io/en/stable/reference/ImageGrab.html?highlight=imagegrab) – stovfl Jan 18 '20 at 10:27
  • that's what exactly my point is. I have already implemented this in windows now I want to develop a Linux version, ImageGrab does not work in Linux. – Ambrish Dhaka Jan 18 '20 at 11:20

0 Answers0