3

I know how to extract text from images using preview.app, however I would like to use this feature from terminal to extract all the text from a bunch of images. I tried this using this method to apply same operation to multiple images but it doesn't work (it select text from only one image at time).

I also tried converting the images into one pdf, but the text selection doesn't work anymore.

I found no macOS apps or utilities I could use for extract the text from my image(s) from terminal.

Right now I used the proprietary Apple's text recognition machine learning new capabilities (on macOS and iOS, here it's explained how to use their Vision framework).

Any idea?

4
  • 2
    How exactly did you apply the linked method to extract text? Can you loop over all the images, collecting the extracted text in a text file?
    – nohillside
    Dec 13, 2022 at 11:39
  • Of course I could loop over the images, but I found no macos apps or utilities I could use for this from terminal (I edited my question to explain what I tried). Dec 13, 2022 at 13:46
  • 1
    I was referring to "I tried this using this method to apply same operation to multiple images but it doesn't work (it select text from only one image at time)". How exactly did you extract the text of one image?
    – nohillside
    Dec 13, 2022 at 14:11
  • I added an explanation. Right now I used the proprietary Apple's text recognition machine learning new capabilities Dec 13, 2022 at 16:17

2 Answers 2

4

In terminal:

brew install tesseract <- this installs the program you need (I assume that you have brew installed, if not, go to this website: https://brew.sh/).

For one image you do this: tesseract my.png textfile

And you get a textfile.txt from that image. It should work with other image formats as well.

In order to loop over all images in some directory, you can do this:

for x in ~/Desktop/*.png; do
    tesseract ${x} ${x} 
done

In this case I am looping over all the png images on my Desktop and creating a .txt file for each one of them.

If you want more formats you can specify them like so:

~Desktop/(*.png|*.jpeg|*.jpg|*.bmp)

8
  • Thanks, I will try this. Howerver I would like to understand if there is any way to use the Apple ML api with some standalone terminal utilities. Dec 13, 2022 at 16:20
  • How do you expect to use an ML API from the command line? It sounds strange. I think you have to write a Swift application in Xcode with apple's vision framework, or something. Why do you need to use an apple api specifically, if tesseract gets the job done? Tesseract seems to be the most popular OCR tool, even iOS developers use it in their apps. It is also developed by google, so I think its quality should be good. Dec 13, 2022 at 18:05
  • 1
    talk is cheap, so here is an example of github.com/xulihang/macOCR , here I took image and detected text in it and then overlayed that text on to the same image and filtered where model is at least 50% confident , if you can detect text with the same quality using tessaract -props to you then, but I doubt bernatskyys.s3.eu-west-1.amazonaws.com/… May 2 at 15:34
  • 1
    @ArtemBernatskyi cool, maybe your should post another answer on here. May 3 at 20:24
  • 1
    Tesseract was the best option so far. It did a much better job than Preview!
    – cptully
    Sep 21 at 13:40
4

I did this using Shortcuts on Mac OS, creating the following shortcut:

A screenshot of the shortcut recipe

Then executing the shortcut from the command line or a script:

shortcuts run "extract_text_from_image" -i input.png -o output.txt

I played with Tesseract too, but it seems more focused on the traditional OCR case (extract text from a scanned document or book), this method seems to do way better extracting random text from images.

New contributor
Todd W is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • I use the same trick to automatically sort incoming invoices to relevant files.
    – Hugues
    Nov 25 at 14:47
  • I will try this also, thank you Nov 25 at 23:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .