Python提取PDF中的图片并构建Gui框架
使用一段python代码实现在GUI窗口中完成对pdf中的图片的提取
一.python实现对pdf图片提取
1. 使用pip安装pymupdf
pip install pymupdf
可以使用pip list
查看是否安装成功以及已安装的python包
2.导入相关库
import fitz
import time
import re
import os
3.使用函数完成具体实现
参考https://blog.csdn.net/qq_15969343/article/details/81673302
def pdf2pic(path, pic_path):
#计时
t0 = time.perf_counter()
# 使用正则表达式来查找图片
checkXO = r"/Type(?= */XObject)"
checkIM = r"/Subtype(?= */Image)"
# 打开pdf
doc = fitz.open(path)
# 图片计数
imgcount = 0
lenXREF = doc.xref_length()
# 打印PDF的信息
print("文件名:{}, 页数: {}, 对象: {}".format(path, len(doc), lenXREF - 1))
# 遍历每一个对象
for i in range(1, lenXREF):
# 定义对象字符串
text = doc.xref_object(i)#
isXObject = re.search(checkXO, text)
# 使用正则表达式查看是否是图片
isImage = re.search(checkIM, text)
# 如果不是对象也不是图片,则continue
if not isXObject or not isImage:
continue
imgcount += 1
# 根据索引生成图像
pix = fitz.Pixmap(doc, i)
# 根据pdf的路径生成图片的名称
# new_name = path.replace('\\', '_') + "_img{}.png".format(imgcount)
new_name = "img{}.png".format(imgcount)
new_name = new_name.replace(':', ''