import fitz
import os
from PIL import Imagepath1 = "D:/桌面/xxxxxx.pdf"
path2 = "D:/桌面/111"def pdf2image1(path1, path2):pdfDoc = fitz.open(path1)for pg in range(pdfDoc.page_count):page = pdfDoc.load_page(pg)# 获取页面的图像对象# matrix = fitz.Matrix(1.0, 1.0) # 1.0 表示原始尺寸# pix = page.get_pixmap(matrix=matrix,dpi=200)pix = page.get_pixmap(matrix=fitz.Matrix(4, 4))print(pix.width, pix.height)# 将图像转换为Pillow的Image对象img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)if not os.path.exists(path2): # 判断存放图片的文件夹是否存在os.makedirs(path2) # 若图片文件夹不存在就创建# 保存图像为PNG格式,不进行压缩# dpi = 96 # 设置所需的 DPI 值img.save(path2 + '/' + f'images_{pg}.jpg', )# img.save(f'output_{page_number}.png',)# pix.save(path2 + '/' + 'images_%s.png' % pg) # 将图片写入指定的文件夹内if __name__ == "__main__":pdf2image1(path1, path2)