import pytesseract
from PIL import Image
import pyautogui
import mss
import time
# Define the text you are looking for
TARGET_TEXT = "YourTargetText"
# Function to capture the screen and return the image
def capture_screen():
with mss.mss() as sct:
monitor = sct.monitors[1] # Capture the first monitor (adjust if needed)
screenshot = sct.grab(monitor)
img = Image.frombytes("RGB", (screenshot.width, screenshot.height), screenshot.rgb)
return img
# Function to detect text using pytesseract
def detect_text(image):
text = pytesseract.image_to_string(image)
return text
# Main loop to continuously check for the text and press spacebar
while True:
screen_image = capture_screen()
detected_text = detect_text(screen_image)
# If the target text is detected, press the spacebar
if TARGET_TEXT in detected_text:
print(f"Detected '{TARGET_TEXT}', pressing spacebar!")
pyautogui.press('space')
time.sleep(1) # Adjust the sleep time as needed