用到的库: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)