Python 将pdf转成图片的方法

yipeiwu_com5年前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设计】。

相关文章

手动安装python3.6的操作过程详解

手动安装python3.6只需要将其ppa源加入apt仓库列表即可,但是最近常用的一个源 ppa:jonathonf/python-3.6 突然403拒绝访问了,费劲千辛万苦终...

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换) Python3 JSON 数据解析...

Win10环境python3.7安装dlib模块趟过的坑

Win10环境python3.7安装dlib模块趟过的坑

在头条看了一篇文章,说五行代码实现人脸识别,一时感兴趣了,来搞搞 先是按照文章说的 操作了几步,到后面虽然,import dlib 不报错,但是 代码里面运行的时候 detector...

神经网络(BP)算法Python实现及应用

神经网络(BP)算法Python实现及应用

本文实例为大家分享了Python实现神经网络算法及应用的具体代码,供大家参考,具体内容如下 首先用Python实现简单地神经网络算法: import numpy as np #...

一看就懂得Python的math模块

math模块 # 数学相关模块 import math r = math.floor(3.2) # 向下取整 print(r) r = math.ceil(4.5) # 向上取整...