步驟:
1.需要安裝ImageMagick
2.安裝Ghostscript (GS)
3.安裝Wand
開始實現:
1.安裝ImageMagick (由於brew預設7.x會有問題,所以強制安裝6.x)
1 |
brew install imagemagick@6 |
2.連結6.x版本
1 2 |
brew link imagemagick@6 --force |
3.設定export環境變數
1 2 |
<span class="hljs-built_in">echo</span> <span class="hljs-string">'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"'</span> >> ~/.bash_profile |
4.安裝GS與wand
1 |
brew install gs</code><code> |
1 |
pip3 install wand |
5.撰寫&執行
1 2 3 4 5 6 7 8 9 10 11 |
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") |