pdf文件转化为png照片 (PyMuPDF下面的fitz)
1.本篇给大家封装了一个函数,用来将pdf转化为png图片,可以转化全部,也可以转化单页;
代码如下:
import fitz import os def pyMuPDF_fitz(pdfPath, imagePath, num, all=True): """ :param pdfPath: pdf文件路径 :param imagePath: 图片存放目录 :param num: 第几页,索引从0开始 :param all: 是否转化所有页面 :return: """ pdfDoc = fitz.open(pdfPath) if all: start = 0 end = pdfDoc.pageCount else: start = num end = num+1 for i in range(start,end): page = pdfDoc[i] rotate = int(0) zoom_xy = (1.3333,1.3333) mat = fitz.Matrix(zoom_xy[0],zoom_xy[1]).prerotate(rotate) pix = page.getPixmap(matrix=mat,alpha=False) if not os.path.exists(imagePath): os.makedirs(imagePath) pix.writePNG(f"{imagePath}{i}.png") pdfPath = 'E://a.pdf' imagePath = 'E://' pyMuPDF_fitz(pdfPath, imagePath ,0,False)
2.此模块可以直接裁剪图片,然后转成字节类型
pdf_doc = fitz.Document(stream=pdf_req, filetype="pdf") page = pdf_doc[num] mat = fitz.Matrix(zoom_x, zoom_y).preRotate(0) clip = fitz.Rect(Point(coordinate[0] / k_y, coordinate[1] / k_x), Point(coordinate[2] / k_y, coordinate[3] / k_x)) pix = page.getPixmap(matrix=mat, clip=clip) img_bytes = pix.getImageData() return BytesIO(img_bytes)
-----------------------------------------------------------------------------------------------------------------------------------------