We write a simple translator of texts from pictures and screenshots in Python

If you like to code and play a little bit more text games that are often not in your language, then you can make yourself a translator in an hour with a few easy-to-use libraries. He will be able to translate either from a predetermined area of the screen where the text appears, or from the clipboard. We start the game in windowed mode, so that the console with our program is also visible and press Win + Shift + S (this allows you to select an area for Win10) and then press F9 – it works out the translation script, we look at the translation.

So we need a library for capturing a picture from the screen or an area – pyscreenshot. To capture an image from the clipboard, we take ImageGrab from the PIL library, in order not to switch every time to our script, but it could work in the background – we take the keyboard library, well, for The translation itself has the googletrans library. Now the script itself:

from googletrans import Translator  # text translation via google translate
import pyscreenshot                 # screen capture
import easyocr                      # text recognition from image
from PIL import ImageGrab           # to capture image from clipboard
import keyboard                     # to capture keystrokes in the background

########### Here is the utility settings ########################################
src_lang = 'en'                     # original text language
dst_lang = 'ru'                     # target language for translation
key_for_clipboard = 'f9'            # button for transferring pictures from the clipboard
key_for_screencapture = 'f12'       # button to translate screen area
screen_region = (150,850,1400,1000) # screen area, coordinates of capture window borders (Left, Top, Right, Bottom)

translator = Translator()
ocrreader = easyocr.Reader([src_lang])

def image_translate():
    """Translation of an already saved image image_to_translate.png"""
    global ocrreader
    print('')
    # Image text recognition
    result = ocrreader.readtext('image_to_translate.png', detail=0)
    resulttext = ' '.join(result)
    print('\033[92m' + resulttext + '\033[0m')
    # Translation and output of the result
    translated = translator.translate(resulttext, src=src_lang, dest=dst_lang)
    print(translated.text)

def capture_translate():
    """Screenshot and translation"""
    try:
        screenshot=pyscreenshot.grab(bbox=screen_region)
        screenshot.save('image_to_translate.png', format='png')
        image_translate()
    except Exception:
        print('Mistake')

def clipboard_translate():
    """Translation of the image on the clipboard"""
    im = ImageGrab.grabclipboard()
    if im==None:
        print('Copy the image to clipboard first')
    else:
        try:
            im.save('image_to_translate.png','png')        
            image_translate()
        except Exception:
            print('Ошибка')

keyboard.add_hotkey(key_for_screencapture, capture_translate)
keyboard.add_hotkey(key_for_clipboard, clipboard_translate)
print(f'To capture an area of the screen, press {key_for_screencapture}')
print(f'To translate an image from the clipboard, click {key_for_clipboard}')
keyboard.wait()

Create a project folder and create a virtual environment in it. I do this under Windows, yours may be different. I open the console, move to this folder and execute:

python -m venv env

Activate

env\Scripts\activate

Install all packages one by one:

pip install googletrans==3.1.0a0
pip install pyscreenshot
pip install easyocr
pip install keyboard

Then you can simply save the above source code, say, to the translate.py file and run it.

There is one huge disadvantage of this translator – the easyocr library pulls a bunch of dependencies, including a library for working with neural networks, so the folder will swell up to two gigabytes. Well, well, this is the fee for the fact that we can generally write such a program easily.

Leave a Comment

Your email address will not be published.