Skip to content

Replace images of text on the clipboard with the text itself

License

Notifications You must be signed in to change notification settings

benglazer/clipocr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

clipocr

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.

Installation

  1. Install the tesseract OCR engine. On MacOS, assuming you have homebrew installed:
brew install tesseract
  1. Clone the clipocr git repo and enter the project directory:
git clone https://github.com/benglazer/clipocr.git
cd clipocr
  1. (optional) Create a virtualenv to isolate clipocr's environment. Assuming you have pyenv with virtualenv support already installed:
pyenv virtualenv clipocr
pyenv local clipocr
  1. From the project directory, install Python dependencies:
pip install -r requirements.txt

MacOS shortcut assignment

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.

  1. Open Automator.
  2. Select Quick Action from the New Workflow menu that appears on load.
  3. Set "Workflow receives current" to "no input".
  4. Drag "Run Shell Script" from the Actions library to the workflow stage.
  5. 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
  1. Save the Quick Action and name it "clipocr".

Step 2: Attach the new Quick Action to a Keyboard Shortcut.

  1. Open System Settings.
  2. Navigate to the Keyboard section.
  3. Click the "Keyboard Shortcuts..." button.
  4. Locate "clipocr" under Services->General.
  5. Select the checkbox on the left.
  6. Double click "none" (or existing shortcut) to its right supply a keyboard shortcut; I use ⌃⌥⇧⌘O (ctrl-alt-shift-meta-O).

Usage

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.

About

Replace images of text on the clipboard with the text itself

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages