pdf转换成jpg python_Python将PDF转成图片

新建pdfconverter.py 文件,并将以下代码复制粘贴:

# -*- coding: utf-8 -*-

import io

from wand.image import Image

from wand.color import Color

from PyPDF2 import PdfFileReader, PdfFileWriter

memo = {}

def getPdfReader(filename):

reader = memo.get(filename, None)

if reader is None:

reader = PdfFileReader(filename, strict=False)

memo[filename] = reader

return reader

def _run_convert(filename, page, res=120):

idx = page + 1

pdfile = getPdfReader(filename)

pageObj = pdfile.getPage(page)

dst_pdf = PdfFileWriter()

dst_pdf.addPage(pageObj)

pdf_bytes = io.BytesIO()

dst_pdf.write(pdf_bytes)

pdf_bytes.seek(0)

img = Image(file=pdf_bytes, resolution=res)

img.format = 'png'

img.compression_quality = 90

img.background_color = Color("white")

img_path = '%s%d.png' % (filename[:filename.rindex('.')], idx)

img.save(filename=img_path)

img.destroy()

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页