前言
最近項(xiàng)目需要pdf中提取內(nèi)容,pdf是掃描版,想通過轉(zhuǎn)成圖片,通過圖像識(shí)別區(qū)分出段落,然后進(jìn)行ocr識(shí)別,得到結(jié)構(gòu)化數(shù)據(jù)
所以第一步需要搞定的就是pdf轉(zhuǎn)圖片了
環(huán)境: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")