I want to be able to take a screenshot on my mac, then send that to an OCR engine and get back the text it has identified.
Are there any existing software tools that do this for macOS?
I have used FastScripts to assign a keyboard shortcut to these commands:
screencapture -i /tmp/tesseract.png
tesseract /tmp/tesseract.png stdout|tr -d \\f|LC_CTYPE=UTF-8 pbcopy
brew install tesseract
installs Tesseract. Tesseract prints to STDOUT when the name of the output file is specified as stdout
. A form feed character is added to the end of the output.
For non-English text, you have to manually specify the language: for example -l deu
for German. To list the available languages, run tesseract --list-langs
.
Edit: In cases where Tesseract produces inaccurate results, I now use Google's Cloud Vision API instead. Demo: https://cloud.google.com/vision/docs/drag-and-drop.
curl -sH"Authorization:Bearer $(gcloud auth application-default print-access-token)" -HContent-Type:application/json\;charset=utf-8 https://vision.googleapis.com/v1/images:annotate -d@<(printf %s '{"requests":[{"image":{"content":"'"$(base64 input.png)"'"},"features":[{"type":"TEXT_DETECTION"}]}]}')|jq -r '.responses[0].fullTextAnnotation.text'
Edit 2: My free trial for the Google API expired, so I now use Yandex Translate to do OCR for images that Tesseract has trouble with: https://translate.yandex.com/ocr.
Edit 3: You can do this to also translate the text (https://cloud.google.com/translate/docs/basic/translating-text):
curl -HAuthorization:Bearer\ $(gcloud auth application-default print-access-token) -HContent-Type:application/json -d@<(jq -sR '{q:.,target:"de"}'<<<example) https://translation.googleapis.com/language/translate/v2|jq -r '.data.translations[0].translatedText'
tesseract
, you can avoid temporary file by using clipboard as a buffer and pngpaste
from homebrew. screencapture -ic && pngpaste - | tesseract stdin stdout | tr -d \\f | pbcopy
Commented
Feb 8, 2021 at 22:31
export PATH=/usr/local/bin:$PATH
for homebrew installed commands to work. Then set up a keybind in macOS "Keyboard" > "Shortcuts" > "Services".
Commented
Feb 8, 2021 at 22:37
TextSniper is another app that does screenshot OCR. It recognizes any text from screenshot and copies editable text to clipboard. Does not require internet connection and is lighting fast. Another great feature of TextSniper is that it can turn recognized text to speech, really helpful for people who have difficulties reading text from screen.
I do it with LEADTOOLS OCR. Haven't used it much. Easy to use. Just used it on a smaller image... a screenshot of a book with both pages in the shot... this made the text smaller, contributing to errors.
Not sure if it's still available. The copyright on LEADTOOLS OCR is 1991-2014, and it is Version 2.
Good luck!
Easy Screen OCR for Mac is a free program that you can have a try.
You could set screencapture to save to a specific folder, then use Folder Actions to have that trigger an AppleScript, which simply sends your picture to your OCR software, either an app or command line.
Of course, this would send every screenshot to the app, which may not be ideal.
EDIT: Monterey now includes the ability to extra from images, which can be automated in Shortcuts.
Here is a command to do it that easily does it
screencapture -i /tmp/tesseract.png; tesseract -l eng /tmp/tesseract.png /tmp/text\ document; open -R /tmp/text\ document.txt
Download tesseract here