2019年3月

环境配置(mac)
安装ImageMagick
brew install imagemagick
这里有个坑,brew安装都是7.x版本,使用wand时会出错,需要你安装6.x版本。
解决办法:
1.安装6.x版本
brew install imagemagick@6
2.取消链接7.x版本
brew unlink imagemagick
Unlinking /usr/local/Cellar/imagemagick/7.0.7-4… 71 symlinks removed
3.强制链接6.x版本
brew link imagemagick@6 --force
Linking /usr/local/Cellar/imagemagick@6/6.9.9-15… 75 symlinks created
4.export环境变量
echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.bash_profile
ok,以上解决imagemagick版本问题。

安装gs
必须安装gs,否则pdf无法转换。
brew install gs
安装wand
pip3 install wand
我这里使用的是python3,所以需要用pip3.

from wand.image import Image

def convert_pdf_to_jpg(filename):

with Image(filename=filename) as img :
    print('pages = ', len(img.sequence))

    with img.convert('jpeg') as converted:
        converted.save(filename='image/page.jpeg')

安装PyOCR
pip3 install pyocr
txt = tool.image_to_string(

Image.open('test.png'),
lang=lang,
builder=pyocr.builders.TextBuilder()

)

txt is a Python string

brew install leptonica

//安装tesseract的同时安装训练工具
brew install --with-training-tools tesseract

//安装tesseract的同时安装所有语言,语言包比较大,如果安装的话时间较长,建议不安装,按需选择
brew install --all-languages tesseract

//安装tesseract,并安装训练工具和语言
brew install --all-languages --with-training-tools tesseract

//只安装tesseract,不安装训练工具
brew install tesseract

2.下载语言库
下载地址:https://github.com/tesseract-ocr/tessdata
根据自己的需求选择所要的语言库,在这里我们选择的是简体中文所以选择的库是:chi_sim.traineddata、eng.traineddata
将文件拷贝到到:/usr/local/Cellar/tesseract/xxxx/share/tessdata目录下。
3.Tesseract使用
终端输入命令:tesseract --help
一般使用:
//默认使用eng文字库, imgName是图片的地址,result识别结果

tesseract imgName result

指定语言:

//指定使用简体中文
tesseract -l chi_sim imgName result
//查看本地存在的语言库
tesseract --list-langs

指定多语言:

//指定多语言,用+号相连
tesseract -l chi_sim+eng imgName result

有个地方需要特别注意,参数psm

//输入命令,查看psm的参数
tesseract --help-psm