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计算斗牛游戏概率算法实例分析

本文实例讲述了Python计算斗牛游戏概率算法。分享给大家供大家参考,具体如下: 过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛。在这些游戏中,斗牛是最受欢迎的,因为可...

python 除法保留两位小数点的方法

如下所示: a = 1 b = 3 print(a/b) #方法一: print(round(a/b,2)) #方法二: print(format(float(a)/float(b)...

详谈Numpy中数组重塑、合并与拆分方法

1.数组重塑 1.1一维数组转变成二维数组 通过reshape( )函数即可实现,假设data是numpy.array类型的一维数组array([0, 1, 2, 3, 4, 5, 6,...

python3 dict ndarray 存成json,并保留原数据精度的实例

如下所示: import numpy as np import codecs, json a = np.arange(10).reshape(2,5) # a 2 by 5 a...

Python中的字符串切片(截取字符串)的详解

Python中的字符串切片(截取字符串)的详解

字符串索引示意图 字符串切片也就是截取字符串,取子串 Python中字符串切片方法 字符串[开始索引:结束索引:步长] 切取字符串为开始索引到结束索引-1内的字符串 步长不指定时步长为...