python pdf转为图片

在python中用到pdf转图片,实现如下:

  • 安装wand
pip install wand
  • 安装imagemagick
    Mac安装:

    brew install freetype imagemagick
    

    ubuntu安装 :

    sudo apt-get install libmagickwand-dev
    

    CentOS安装:

    yum install ImageMagick-devel
    

安装详情请看这里

  • 代码片段
from wand.image import Image
import os
# 将pdf文件转为jpg图片文件
cur_file_path = os.path.dirname(os.path.realpath(__file__))
# path为pdf文件路径
path = os.path.join(cur_file_path, os.pardir, 'ehouse/resource/img/')
image_pdf = Image(filename=path + 'e宅快贷业务申请表.pdf', resolution=300)
image_jpeg = image_pdf.convert('jpg')

# 页面转为独立的二进制图像对象
req_image = []
for img in image_jpeg.sequence:
    img_page = Image(image=img)
    req_image.append(img_page.make_blob('jpg'))

# 存储为图片文件
i = 0
for img in req_image:
    ff = open(path + e宅快贷业务申请表+ str(i) + '.png', 'wb')
    ff.write(img)
    ff.close()
    i += 1

去path文件下查看文件吧~

猜你喜欢

转载自blog.csdn.net/quanqxj/article/details/89314334