Convert python pdf to image

Using pdf to convert pictures in python, the implementation is as follows:

  • install wand
pip install wand
  • Install imagemagick
    Mac installation:

    brew install freetype imagemagick
    

    ubuntu installation:

    sudo apt-get install libmagickwand-dev
    

    CentOS installation:

    yum install ImageMagick-devel
    

Please click here for installation details

  • code segment
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

Go to the path file to view the file~

Guess you like

Origin blog.csdn.net/quanqxj/article/details/89314334