前言
最近项目需要 PDF 中提取内容, PDF 是扫描版, 想通过转成图片, 通过图像识别区分出段落, 然后进行 ocr 识别, 得到结构化数据
所以第一步需要搞定的就是 PDF 转图片了
环境: Mac 10.12.6 (16G29)
正文
安装依赖
注意 ImageMagick, 目前不支持最新的 7 版本, 所以只能装 6
- brew install freetype
- brew install GhostScript
- brew install ImageMagick@6
- brew link --overwrite ImageMagick@6
- echo 'export MAGICK_HOME=/usr/local/opt/imagemagick@6'>> ~/.bash_profile
- echo 'export PATH="$MAGICK_HOME/bin:$PATH"'>> ~/.bash_profile
- pip install Wand
python 脚本
- from wand.image import Image
- # Converting first page into JPG
- with Image(filename="/thumbnail.pdf[0]") as img:
- img.save(filename="/temp.jpg")
来源: https://www.qcloud.com/developer/article/1359230