Skip to content

Instantly share code, notes, and snippets.

@henrik
Created March 3, 2012 17:07
Show Gist options
  • Save henrik/1967035 to your computer and use it in GitHub Desktop.
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

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

@GregBaugues
Copy link

Also thank you. This was great.

@coolya
Copy link

coolya commented Oct 8, 2015

👍

@mikedewar
Copy link

👍

@sterlingwes
Copy link

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

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

@stoivo
Copy link

stoivo commented Apr 26, 2016

Is there a way I can install only norwegian language?

@flyingmrwang
Copy link

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?

Copy link

ghost commented May 31, 2016

thanks a lot

Copy link

ghost commented Jul 13, 2016

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

@yangboz
Copy link

yangboz commented Nov 22, 2016

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

@ekovacs
Copy link

ekovacs commented Jan 30, 2017

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

@Digital2Slave
Copy link

👍 😄

@Sentinel-Prime
Copy link

Thanks A lot , you saved my life.

@Sentinel-Prime
Copy link

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

@DJLunacy
Copy link

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

@BVijayKrishna
Copy link

It's working thanks for the help

@HjoshM
Copy link

HjoshM commented Feb 16, 2018

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

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

@olimorris
Copy link

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
Copy link

sshaw commented Nov 24, 2018

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

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

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
Copy link

varenc commented Feb 6, 2019

@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

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

@Anima-t3d
Copy link

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
Copy link

bejvisek commented May 22, 2019

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

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

thanks @bejvisek

@JamesAsuraA93
Copy link

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