Skip to content Skip to sidebar Skip to footer

Python & OpenCV

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 l

Solution 1:

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)

enter image description here


Solution 2:

I've grabbed the images from an pdf containing images as well as text.

You can save the images using pix.writePNG() or just show it using cv2.imshow(), whichever suits you best.

import fitz    #pymupdf
from cv2 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

def convertPdf(filename):  
    doc = fitz.open(filename)
    #count = 0
    for i in range(len(doc)):
        for img in doc.getPageImageList(i):
            xref = img[0]
            pix = fitz.Pixmap(doc, xref)

            #if pix.n < 5:       # this is GRAY or RGB
            # To save it to the disk
            #pix.writePNG(f"p{count}.png")

            im = pix2np(pix)
            cv2.imshow("image",im)
            cv2.waitKey(0)
            #count += 1
            pix = None

if __name__ == "__main__":
    filename = "sample.pdf"
    convertPdf(filename)

Post a Comment for "Python & OpenCV"