From fdc2fdc41d599c6cdeae2a2d185d92a7fa5316bf Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Sat, 31 May 2025 20:13:34 -0400 Subject: [PATCH] Ocr now copies detected text to the clipboard. --- scripts/ocr.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/scripts/ocr.py b/scripts/ocr.py index b69c10c..ce91ab0 100755 --- a/scripts/ocr.py +++ b/scripts/ocr.py @@ -23,6 +23,7 @@ import time import subprocess from PIL import Image, ImageOps import pytesseract +import pyperclip def capture_screen(max_retries=3, initial_delay=0.2): """ @@ -102,6 +103,22 @@ def perform_ocr(img, lang='eng'): return text +def copy_to_clipboard(text): + """Copy text to clipboard using pyperclip""" + try: + # Filter out empty lines and clean up the text + lines = [line.strip() for line in text.split('\n') if line.strip()] + cleaned_text = '\n'.join(lines) # Preserve line breaks for clipboard + + if cleaned_text: + pyperclip.copy(cleaned_text) + return True + else: + return False + except Exception as e: + print(f"Error copying to clipboard: {e}") + return False + def speak_text(text): """Speak the text using speech-dispatcher""" # Filter out empty lines and clean up the text @@ -131,9 +148,12 @@ def main(): # Perform OCR text = perform_ocr(processed_img) + # Copy to clipboard + clipboard_success = copy_to_clipboard(text) + # Speak the results speak_text(text) - + except Exception as e: # Let the user know something went wrong error_msg = f"Error during OCR: {str(e)}"