Python教程

python 从pdf中提取图片

本文主要是介绍python 从pdf中提取图片,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

需要用到 pymupdf 库

安装

pip install PyMuPDF

 

创建py文件

import fitz

pdf_document = fitz.open("12.pdf") #文件位置
for current_page in range(len(pdf_document)):
    print(current_page)
    for image in  pdf_document.get_page_images(current_page):
        print(image)
        xref = image[0]
        pix = fitz.Pixmap(pdf_document,xref)
        if pix.n<5:
            pix.save('imgs/page%s-%s.jpg' %(current_page,xref)) #保存地址
        else:
            pix1 = fitz.Pixmap(fitz.csRGB,pix)
            pix1.save('imgs/page%s-%s.jpg' %(current_page,xref)) #保存地址
            pix1=None
        pix = None

  

这篇关于python 从pdf中提取图片的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!