Python 将pdf转成图片的方法

yipeiwu_com6年前Python基础

本篇文章记录如何使用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 imagemagick
Unlinking /usr/local/Cellar/imagemagick/7.0.7-4… 71 symlinks removed

3.强制链接6.x版本

 brew link imagemagick@6 --force
Linking /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 Image
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='image/page.jpeg')

效果

笔者将一本书四百多页都转出来了,大家也可以去试下啦。

以上这篇Python 将pdf转成图片的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现log日志的示例代码

源代码: # coding=utf-8 import logging import os import time LEVELS={'debug':logging.DEBUG,\...

python3实现字符串的全排列的方法(无重复字符)

最近在学一些基础的算法,发现我的数学功底太差劲了,特别是大学的这一部分,概率论、线性代数、高数等等,这些大学学的我是忘得一干二净(我当时学的时候也不见得真的懂),导致现在学习算法,非常的...

Python3 main函数使用sys.argv传入多个参数的实现

在运维过程中,有些时候需要向main函数中传递参数,以方便运维与测试,那么怎么向main函数中传入多个参数呢,下面以python3中的main函数为例,简单讲一下。 首先我们需要impo...

详解用TensorFlow实现逻辑回归算法

详解用TensorFlow实现逻辑回归算法

本文将实现逻辑回归算法,预测低出生体重的概率。 # Logistic Regression # 逻辑回归 #---------------------------------- #...

python logging添加filter教程

例子一 def filter(self, record): """Our custom record filtering logic. Built-in filter...