Copy an image to the clipboard, then execute an Alfred workflow to trigger OCR on the copied image. Copy the text returned by the webservice to the clipboard.
Retrieving anything but text from the (OS X) keyboard in Python seems troublesome. The CLI command pbpaste
and the Python libraries xerox
and pyperclip
only support text. Judging from posts on StackOverflow, it should be possible using PyObjC
or Tkinter
but I couldn't get the former to install and the latter seems a bit much just to get access to the clipboard.
Instead, I used the CLI utility pngpaste
, which aims to do what pbpaste
does, but for binary data. By using -
as parameter instead of a filename, I can retrieve the binary data from stdout
into Python.
The API's requirements state that the image must be at least 40x40 pixels. If not, the server returns an HTTP 500
error. Therefore, we need to use PIL to check the image dimensions and change them to meet the requirements if the dimesions are smaller than 40px.