其他分享
首页 > 其他分享> > 提取PDF文件中的图片

提取PDF文件中的图片

作者:互联网

# -*- coding: utf-8 -*-
# pip install pyMuPDF 安装pyMuPDF库
import fitz
import io
from PIL import Image


def img():
    pdf_file = fitz.open("file/7_2.pdf")
    for page_no in range(len(pdf_file)):
        curr_page = pdf_file[page_no]
        images = curr_page.geImageList()
        # 迭代处理PDF文档中的图片
        for num, image in enumerate(curr_page.geImageList()):
            # 获取图片的XREF
            xref = image[0]
            # 提取图片的字节
            curr_image = pdf_file.extractImage(xref)
            img_bytes = curr_image['image']
            # 获取图片的扩展名
            img_extension = curr_image['ext']
            image = Image.open(io.BytesIO(img_bytes))
            # 将图片保存在本地
            image.save(open(f"file/第{page_no +1}页-第{num + 1}张图.{img_extension}", "wb"))


img()

 

标签:提取,curr,img,image,file,pdf,PDF,page,图片
来源: https://www.cnblogs.com/zhaoyiguang/p/16671265.html