Hu知非

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

用到的库:pymupdf,PIL

将pdf的多页合并成一个jpg图片

import fitz
from PIL import Image

def convert_img(pdf, jpg):
    doc = fitz.open(pdf)
    img_bytes = b''
    height = 0
    width = 0
    samples = b''
    dpi = ()
    for pg in range(doc.pageCount):
        page = doc[pg]
        rotate = 0
        #放大分辨率
        zoom_x = 2.0
        zoom_y = 2.0
        trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
        pix = page.getPixmap(matrix=trans, alpha=False)
        height += pix.height
        width = pix.width
        samples += pix.samples
        dpi = (pix.xres, pix.yres)
        img_bytes += pix.getPNGdata()
    img = Image.frombytes('RGB', (width, height), samples)
    img.save('new_image.jpg', dpi=dpi)

if __name__ == '__main__':
    convert_img('test.pdf', 'test.jpg')

参考:Python将PDF转成图片—PyMuPDF和pdf2image - 云+社区 - 腾讯云 (tencent.com)

posted on 2021-08-18 13:58  Hu知非  阅读(355)  评论(0编辑  收藏  举报