python提取pdf文件中的图片并输出到本地

python 复制代码
import fitz
import os
from PIL import Image

path1 = "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)
相关推荐
金銀銅鐵10 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup1115 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0017 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵19 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf19 小时前
Agent 流程编排
后端·python·agent
copyer_xyf20 小时前
Agent RAG
后端·python·agent
copyer_xyf20 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf20 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python