# -*- coding:utf-8 -*- """ __project_ = 'ToBase64' __file_name__ = 'PdfToImg' __author__ = 'xbxia' __time__ = '2020/10/26 17:45' __product_name = PyCharm """ import os import fitz ''' # 将PDF转化为图片 pdfPath pdf文件的路径 imgPath 图像要保存的文件夹 zoom_x x方向的缩放系数 zoom_y y方向的缩放系数 rotation_angle 旋转角度 ''' def pdf_image(pdfPath, imgPath, zoom_x, zoom_y, rotation_angle): # 打开PDF文件 pdf = fitz.open(pdfPath) # 逐页读取PDF for pg in range(0, pdf.pageCount): page = pdf[pg] # 设置缩放和旋转系数 trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotation_angle) pm = page.getPixmap(matrix=trans, alpha=False) # 开始写图像 pm.writePNG(imgPath + str(pg) + ".png") pdf.close() if __name__ == '__main__': pdfPath = r"D:workspaceToBase64组合图组合 1_光山县交通运输局执法所+22.pdf" imgPath = r"D:workspaceToBase64组合图img" pdf_image(pdfPath, imgPath, 5, 5, 0)