Search code examples
pythonpositionscreenshotpyautoguiregion

how to take a screnshot by Pyautogui automatically and randomly


objective i want to extract text from image. i play a game which an icon appears randomly ,and there is a text(text as image) near to the icon from the right. i want the script take screenshot of the region of the text only. so, i want the script every time he locatonscreen the i con, i want him take screen shot of the text. here is an image to understand the idea : enter image description here

this is my code:

import pyautogui as py
import time
from PIL import Image
from pytesseract import *

pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

while 1:
        indice1 = py.locateOnScreen("icon.png")
    
    if indice1:
        
        print("indice see it ")

        myScreenshot = py.screenshot()
        myScreenshot.save(r'C:\Users\rachidel07\Desktop\ok\venv\image.png')

        img=Image.open("image.png")
        output = pytesseract.image_to_string(img)
        print(output)

    else:
            print ("non")
        



Solution

  • If you just want the text, check for the icon and when it does find it, take a picture of the whole box with coordinates relative to the icon (you get this easily get this since locateonscreen returns coordinates and you can just measure how big the text box is and do the math.) Then use PIL to crop only the text and then use tesseract for ocr.

    To crop the text, you would use the crop() from PIL.

    from PIL import Image    
    
    img = Image.open("image.png")
    newimg = img.crop((100, 100, 150, 150))
    
    newimg.save("croppedimage.png")