步驟:
1.需要安裝ImageMagick
2.安裝Ghostscript (GS)
3.安裝Wand
開始實現:
1.安裝ImageMagick (由於brew預設7.x會有問題,所以強制安裝6.x)
brew install imagemagick@6
2.連結6.x版本
brew link imagemagick@6 --force
3.設定export環境變數
echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.bash_profile
4.安裝GS與wand
brew install gs
pip3 install wand
5.撰寫&執行
from wand.image import Image
filename = "./檔案名稱.pdf"
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='./page.jpeg') //轉出格式名稱
convert_pdf_to_jpg("./檔案名稱.pdf")