Python 將pdf轉成圖片的方法

來源:互聯網
上載者:User
下面為大家分享一篇Python 將pdf轉成圖片的方法,具有很好的參考價值,希望對大家有所協助。一起過來看看吧

本篇文章記錄如何使用python將pdf檔案切分成一張一張圖片,包括環境配置、版本相容問題。

環境配置(mac)

安裝ImageMagick

brew install imagemagick

這裡有個坑,brew安裝都是7.x版本,使用wand時會出錯,需要你安裝6.x版本。

解決辦法:

1.安裝6.x版本

brew install imagemagick@6

2.取消連結7.x版本

brew unlink imagemagickUnlinking /usr/local/Cellar/imagemagick/7.0.7-4… 71 symlinks removed

3.強制連結6.x版本

 brew link imagemagick@6 --forceLinking /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 Imagedef 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')

效果

筆者將一本書四百多頁都轉出來了,大家也可以去試下啦。



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.