Replace an image containing text on the system clipboard with the text itself.
Tested only on MacOS, but should work across platforms.
Thanks to Google's excellent Tesseract OCR software, the wondrous and powerful Pillow, Pyperclip, and Python Tesseract libraries, as well as the beautiful simplicity of the Python language itself, clipocr's source code is substantially shorter than this README file.
- Install the tesseract OCR engine. On MacOS, assuming you have homebrew installed:
brew install tesseract
- Clone the clipocr git repo and enter the project directory:
git clone https://github.com/benglazer/clipocr.git
cd clipocr
- (optional) Create a virtualenv to isolate clipocr's environment. Assuming you have pyenv with virtualenv support already installed:
pyenv virtualenv clipocr
pyenv local clipocr
- From the project directory, install Python dependencies:
pip install -r requirements.txt
One convenient way to access clipocr
is to attach it to a keyboard shortcut. To do this on MacOS, you can combine Automator with Keyboard Shortcuts, both built into MacOS.
You must first install clipocr
and its dependencies as described above.
Step 1: Create a Quick Action in Automator.
- Open Automator.
- Select Quick Action from the New Workflow menu that appears on load.
- Set "Workflow receives current" to "no input".
- Drag "Run Shell Script" from the Actions library to the workflow stage.
- With /bin/zsh (default) selected as your Shell, enter the command to run clipocr in the command textarea. You'll want to adapt this to your details, but in my case (username "benglazer", pyenv-generated virtualenv "clipocr"), this looks like:
PATH=/opt/homebrew/bin:$PATH /Users/benglazer/.pyenv/versions/clipocr/bin/python /Users/benglazer/dev/benglazer/clipocr/clipocr.py
- Save the Quick Action and name it "clipocr".
Step 2: Attach the new Quick Action to a Keyboard Shortcut.
- Open System Settings.
- Navigate to the Keyboard section.
- Click the "Keyboard Shortcuts..." button.
- Locate "clipocr" under Services->General.
- Select the checkbox on the left.
- Double click "none" (or existing shortcut) to its right supply a keyboard shortcut; I use ⌃⌥⇧⌘O (ctrl-alt-shift-meta-O).
Simply copy an image containing text to your system clipboard and run:
clipocr.py
... or use the shortcut, if you created one.
After executing the script, an image on the clipboard will be replaced with text extracted from that image. If an image was not found, an error is printed.