首页 > 解决方案 > 将 PDF 转换为图像打印输出没有打印功能

问题描述

我找到了一种将 PDF 文件转换为 JPG 的方法,实际上是从 PDF 文件中提取图像文件。PyMuPDF我已经设法用lib做到了。这是该库的文档:

https://pymupdf.readthedocs.io/en/latest/

我看过这段代码:

在python中从PDF中提取图像而不重新采样?

这个代码:

https://www.thepythoncode.com/article/extract-pdf-images-in-python

我写了一个代码,没有给我任何错误,这是代码:

import fitz
import cv2
import numpy as np


doc = fitz.open("sample15.pdf")
#print(doc)

my_images = []
for i in range(len(doc)):

    for img in doc.getPageImageList(i):
        xref = img[0]

        img = doc.extractImage(xref)
        img = img["image"]

        nparr = np.frombuffer(img, np.uint8)
        img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        my_images.append(img_np)

如您所见,我在任何地方都没有打印功能,但我的程序会打印:

mupdf: expected object number #this is printed red
xref 9 image type jpeg
xref 12 image type jpeg
xref 15 image type jpeg
xref 18 image type jpeg
xref 21 image type jpeg
xref 24 image type jpeg

为什么我会得到这个打印输出,我该如何删除它?我猜它来自lib,但我不知道如何阻止它

标签: pythonimagepdfextract

解决方案


该输出可能来自您正在使用的库之一。您可以查看他们的文档以确定是否有日志记录级别选项,或者作为最后的“修复”,使用contextlib.redirect_stdout(和.redirect_stderr)上下文管理器隐藏输出。


推荐阅读