OCR problems

To solve this problem, I wrote a python script that retrieves screen content and performs ocr using the pytesseract module. The script works without a problem. I had some work to do to get it running in python in the environment, but it worked out and I learned a new skill :slight_smile:

Before that, install the module pytesseract in the environment.

Script placed in location
In the KM I launch: "Execute Shell Script"
source /bin/activate
python3 /ScreenOCRtoClipboard.py

Script:

#!/usr/bin/python3

import subprocess
import pytesseract
from PIL import Image
import os

def main():
# Ścieżka do zapisu zrzutu ekranu
screenshot_path = '/temp_screenshot.png'

# Ustaw ścieżkę do wykonywalnego pliku Tesseract OCR
pytesseract.pytesseract.tesseract_cmd = '/opt/homebrew/bin/tesseract'  # Zaktualizuj tę ścieżkę, jeśli to konieczne

try:
    # Wykonaj zrzut wybranego obszaru ekranu
    subprocess.run(['screencapture', '-i', screenshot_path])
    
    # Wykonaj OCR na obrazie w języku polskim
    text = pytesseract.image_to_string(Image.open(screenshot_path), lang='pol')
    
    # Skopiuj tekst do schowka
    process = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
    process.communicate(text.encode('utf-8'))
    
finally:
    # Usuń tymczasowy plik zrzutu ekranu
    if os.path.exists(screenshot_path):
        os.remove(screenshot_path)

if name == 'main':
main()

1 Like