Extracting text from image in macOS from the CLI
Newer versions of macOS has the VNRecognizeTextRequest API. However, I can't find an easy-to-install tool that lets me use this API in a command line. However, this can be worked around thanks to the fact that:
- The Shortcuts app offers the "Extract text from image" action.
- Shortcuts can be run from the command line and can output its result via stdout.
Setting up a shortcut
First, create a shortcut like this:
- Name the shortcut Extract Text
- At the right, under Details, check the following boxes:
- Use as Quick Action
- Provide Output
- In the workflow, add the following actions:
- Receive Images input from Quick Actions
- Extract text from Shortcut Input
- Stop and output Text from Image
Invoking the shortcut
Then, run the shortcut like this:
shortcuts run 'Extract Text' -i <path/to/image.png> | cat
The | cat
is required to make the shortcuts CLI output the result to stdout. Otherwise, it will not output anything.