蟒蛇&OpenCV [英] Python & OpenCV
问题描述
是否有任何本机支持从 PDF 抓取图像或在 Python 中创建某种对象,这些对象可以包含来自 pdf 的图像,然后可以通过 OpenCV 访问?我已经查看了一些脚本来将 PDF 的图像转储到我的目录中,但我的目标更多是访问 PDF,而是将 PDF 中的图像数据转储到我可以访问的某种对象中使用 OpenCV.我自己的探索没有产生任何结果,所以我想我会问.
基于@Ghilas BELHADJ 的示例添加了使用 PyMuPDF 的示例
导入菲茨导入 cv2将 numpy 导入为 np从 tkinter 导入 Tk从 tkinter.filedialog 导入 askopenfilename类 AccessPDF:def __init__(self):self.filepath = ""self.doc = 无def openPDF(self):Tk().withdraw()self.filepath = askopenfilename()self.doc = fitz.open(self.filepath)def pixel2np(self,pix):im = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.h, pix.w, pix.n)im = np.ascontiguousarray(im[..., [2, 1, 0]]) # rgb 到 bgr返回我def displayKey(self):pixobj = self.doc.getPagePixmap(0, alpha=False)im = self.pixel2np(pixobj)cv2.imwrite("testimg.png",im)cv2.imshow("Key" im)
我根据@Dan Mašek 的评论对代码进行了修改
您可以使用
Is there any Native Supports for Grabbing Images from PDFs or Create some sort of Object in Python that can contain the Images from a pdf that then can be access via OpenCV? I've looked at some scripts to dump the Images of a PDF into my directory but I'm aiming more at accessing the PDF and instead dumping the data from the PDF that is the image(s) into some sort of object I can access with OpenCV. My own exploration hasn't yielded any results so i figured I'd ask.
Added a Example of Using PyMuPDF based off example from @Ghilas BELHADJ
import fitz
import cv2
import numpy as np
from tkinter import Tk
from tkinter.filedialog import askopenfilename
class AccessPDF:
def __init__(self):
self.filepath = ""
self.doc = None
def openPDF(self):
Tk().withdraw()
self.filepath = askopenfilename()
self.doc = fitz.open(self.filepath)
def pixel2np(self,pix):
im = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.h, pix.w, pix.n)
im = np.ascontiguousarray(im[..., [2, 1, 0]]) # rgb to bgr
return im
def displayKey(self):
pixobj = self.doc.getPagePixmap(0, alpha=False)
im = self.pixel2np(pixobj)
cv2.imwrite("testimg.png",im)
cv2.imshow("Key" im)
Edit: I've made a modification in the code following the comment of @Dan Mašek
You can achieve this (load the PDF embedded images into OpenCV
without writing intermediate objects on disk) using PyMuPDF
and Numpy
.
In this example, I'm using this pdf file.
import fitz
import cv2
import numpy as np
def pix2np(pix):
im = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.h, pix.w, pix.n)
im = np.ascontiguousarray(im[..., [2, 1, 0]]) # rgb to bgr
return im
doc = fitz.open('NGM_2018_Media_Kit.pdf')
# entire page
# pix = doc.getPagePixmap(0, alpha=False)
# first page , 5th image, xref element
pix = fitz.Pixmap(doc, doc.getPageImageList(0)[4][0])
im = pix2np(pix)
cv2.putText(im, 'Azul fellawen', (100, 100),
cv2.FONT_HERSHEY_SIMPLEX, 1.,
(18, 156, 243), 2, cv2.LINE_AA)
cv2.imwrite('sample_0.png', im)
这篇关于蟒蛇&OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!