Python提取pdf所有页面为图片(PyPDF2 + PhthonMagick + GS方法)

主要参考 https://blog.csdn.net/sweeper_freedoman/article/details/53000145

以下是在Python3.6 64位环境下(Anaconda3安装的)操作

1) 下载PyPDF2源代码,再 pythonsetup.py install 安装源代码

2) 下载PythonMagick的安装包 再pip install 安装包 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonmagick 

3) 没安装gs会提示 RuntimeError: python.exe: PostscriptDelegateFailed `test.pdf': No such file or directory @error/pdf.c/ReadPDFImage/713

4) 安装了gs 还有一个错误 

RuntimeError: Magick: UnableToOpenConfigureFile `delegates.xml' @warning/configure.c/GetConfigureOptions/712

 gs下载地址: https://www.ghostscript.com/download/gsdnld.html

5)针对'delegates.xml', 参考https://blog.csdn.net/sqlserverdiscovery/article/details/51425543

改成

    im = PythonMagick.Image(pdffilename + '[' + str(p) +']')  

    im.density('300')  

     im.write('file_out-' + str(p)+ '.png')  

但是这个时候 im.density('300')设置图像300的dpi的根本没生效,图像保存的尺寸都很小,貌似默认是72dpi的

6)  ★★★★ 后来试出这个方法,  try except 就所有pdf的第一张page会报错,后面的都OK

for p in range(npage): 

    #不加 try except 会报错: RuntimeError: Magick:UnableToOpenConfigureFile `delegates.xml' @warning/configure.c/GetConfigureOptions/712

    try:

        im =PythonMagick.Image() 

       im.density('300')  #设置dpi,不设置估计就96dpi

        im.read(pdffilename+ '[' + str(p) +']') 

        im.write('file_out-'+ str(p)+ '.png') 

    except Exception aserr:    

       print("PythonMagick 产生错误:",end=''),print(err);               

        continue

 7) 目前测试了270份pdf,有6份pdf提取不出图片,部分pdf提取的图片会出现一个大黑块(貌似这种情况下的页面是文本格式的,不是图像格式的页面)

 

主要参考https://blog.csdn.net/sweeper_freedoman/article/details/53000145

次考参考https://blog.csdn.net/sqlserverdiscovery/article/details/51425543 

顺便说下,ImageMagick给保存jpg图片质量提供的默认值是92
详情请移步这个网址 http://www.imagemagick.org/script/command-line-options.php#quality
https://segmentfault.com/q/1010000000593681


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值