Skip to content

Instantly share code, notes, and snippets.

@henrik
Created March 3, 2012 17:07
Show Gist options
  • Select an option

  • Save henrik/1967035 to your computer and use it in GitHub Desktop.

Select an option

Save henrik/1967035 to your computer and use it in GitHub Desktop.
OCR on OS X with tesseract

Install ImageMagick for image conversion:

brew install imagemagick

Install tesseract for OCR:

brew install tesseract --all-languages

Or install without --all-languages and install them manually as needed.

Make sure the input image is a grayscale .tif and fairly large. ~500x150 was too small, while ~2000*500 worked very well.

convert input.png -resize 400% -type Grayscale input.tif

OCR it. The default language is English. Language codes are 3 chars per man tesseract.

tesseract -l eng input.tif output

This creates output.txt.

@brikis98

Copy link
Copy Markdown

Thanks for posting this, saved me lots of time :)

@GregBaugues

Copy link
Copy Markdown

Also thank you. This was great.

@coolya

coolya commented Oct 8, 2015

Copy link
Copy Markdown

👍

@mikedewar

Copy link
Copy Markdown

👍

@sterlingwes

Copy link
Copy Markdown

If you landed here looking to convert a scanned PDF to an OCRable format:

I found that imagemagick's PDF-to-TIFF output was all garbled / distorted. Couldn't find the right flag to increase the resolution, so I tried Ghostscript instead (which imagemagick might use under the hood):

gs -q -r300x300 -dNOPAUSE -sDEVICE=tiffg4 -dBATCH -sOutputFile=output.tif myscan.pdf -c quit

buyer beware, do: man gs first

@pconerly

Copy link
Copy Markdown

FYI, the --all-languages flag has been depreciated in favor of --with-all-languages. Thanks for the guide!

@stoivo

stoivo commented Apr 26, 2016

Copy link
Copy Markdown

Is there a way I can install only norwegian language?

@flyingmrwang

Copy link
Copy Markdown

I have tried as your tutorial. But it shows:
"dyld: Library not loaded: /usr/local/opt/leptonica/lib/liblept.4.dylib
Referenced from: /usr/local/bin/tesseract
Reason: image not found
Trace/BPT trap: 5"
Can anyone tell me how to solve it?

ghost commented May 31, 2016

Copy link
Copy Markdown

thanks a lot

ghost commented Jul 13, 2016

Copy link
Copy Markdown

tesseract: --all-languages was deprecated; using --with-all-languages instead!

@yangboz

yangboz commented Nov 22, 2016

Copy link
Copy Markdown

Warning: tesseract: --all-languages was deprecated; using --with-all-languages instead!

@ekovacs

ekovacs commented Jan 30, 2017

Copy link
Copy Markdown

👍 🥇
thank you! you saved me much frustration and time!!!

@Digital2Slave

Copy link
Copy Markdown

👍 😄

@Sentinel-Prime

Copy link
Copy Markdown

Thanks A lot , you saved my life.

@Sentinel-Prime

Copy link
Copy Markdown

what if I want to convert images to text files in bulk ?

@DJLunacy

Copy link
Copy Markdown

Does converting a png to tiff increase the odds of accurate OCR?

@BVijayKrishna

Copy link
Copy Markdown

It's working thanks for the help

@HjoshM

HjoshM commented Feb 16, 2018

Copy link
Copy Markdown

A long time ago, I installed tesseract 3.05.01 for OCR using HomeBrew:

brew install --with-training-tools tesseract

How do I update it to the latest? I thought by regularly running the following, this would be done:

brew update
brew upgrade
brew outdated

However, my tesseract has not been updated at all...

@kenshinji

Copy link
Copy Markdown

@flyingmrwang did you figure it out? I encountered same issue.

@olimorris

Copy link
Copy Markdown

Love this tip. I created a bash function to make it even easier to run on english text:

function extract-text {
    FILEPATH=$1
    convert $1 -resize 400% -type Grayscale $1.tif
    tesseract -l eng $1.tif output
}

@sshaw

sshaw commented Nov 24, 2018

Copy link
Copy Markdown

For imagemagick: stable 7.0.8-14 on OS X 10.9.5 I had to install with --with-fontconfig:

brew install imagemagick --with-fontconfig

Compare your results with/without resize as resize can take more resources for no OCR improvement.

For OCR improvements see Improving Quality or try training.

@simonkeng

Copy link
Copy Markdown

Is there a brew route for getting & running the latest version of tesseract (LSTM-based, 4.0.0)?

I'm currently using:

$ tesseract --version                                                                                                   
tesseract 3.05.01
 leptonica-1.74.4
  libjpeg 9c : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11

..on macOS, High Sierra 10.13.4 17E202 x86_64. Thanks!

@QuantumQuill

Copy link
Copy Markdown

All languages option is not working

brew install tesseract --all-languages
Error: invalid option: --all-languages

brew --version
Homebrew 2.0.0
Homebrew/homebrew-core (git revision a761; last commit 2019-02-05)
Homebrew/homebrew-cask (git revision 1e6e6; last commit 2019-02-05)

@varenc

varenc commented Feb 6, 2019

Copy link
Copy Markdown

@gabedot

Homebrew recently decided to remove all options from the homebrew-core Formula's. Though as of right now tesseract now includes all languages by default so just remove the option and you should get all languages. This makes tesseract 680MB by default though so think this should change in the future.

In the medium to short term, you can install tesseract with all language support with this
brew install https://github.com/Homebrew/homebrew-core/raw/10708da5492fa4da6fbf2618210681953219409f/Formula/tesseract.rb though that's just a reference to a particular version of the Formula so won't receive future updates.

@Yokileforever

Copy link
Copy Markdown

why my execute brew install tesseract --all-languages is Error: invalid option: --all-languages ?

@Anima-t3d

Copy link
Copy Markdown

why my execute brew install tesseract --all-languages is Error: invalid option: --all-languages ?

Homebrew recently decided to remove all options from the homebrew-core Formula's as explained by @varenc

@bejvisek

bejvisek commented May 22, 2019

Copy link
Copy Markdown

Hi, I have installed tessaract 4.0.0 smoothly by
brew install tesseract
but have only these languages available:
$ tesseract --list-langs List of available languages (3): eng osd snum
No above mention option like --with-all-languates does not work anymore :-/

Is there a way to install selected language(s)? Thanks!

@bejvisek

Copy link
Copy Markdown

Answering my own question:
brew install tesseract-lang
It installs most of the languages, but not all listed here: link
I need to install language "equ", still don't know how

@abdennour

Copy link
Copy Markdown

thanks @bejvisek

@JamesAsuraA93

Copy link
Copy Markdown

MacOS user must use "brew install tesseract-lang" instant "brew install tesseract --all-languages"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment