Unable to extract Qr code from a pdf file in Python , Opencv

Viewed 1429

i am new in Python and Opencv, i wrote code for a simple app that converts pdf to image PNG and then by using opencv extracting the Qr code . it worked for some pdf files that i have done , but i have a specific document pdf that i want to extract the Qr code from it. after the conversion is done i got a message that says no Qr code found!

well this is the only pdf file that is refusing to extract qr code from.

my goal is to find a solution to extract the qr code from that specific pdf file and decode it

here a screen capture for how cv2 selects the qr code after image convertion:

enter image description here

enter image description here

what i have tried in Python

import cv2
import os
from wand.image import Image as wi
PDFfile = wi(filename="Sources/certificatFaresBenslamaAr.pdf",resolution=400)
Images = PDFfile.convert('png')
ImageSequence = 1

for img in PDFfile.sequence:
    Image = wi(image = img)
    Image.save(filename="Image"+str(ImageSequence)+".png")
    ImageSequence += 1

    # read the QRCODE image
image = cv2.imread("image1.png")
# initialize the cv2 QRCode detector
qrCodeDetector = cv2.QRCodeDetector()
# detect and decode
decodedText, points, straight_qrcode = qrCodeDetector.detectAndDecode(image)
#points is the output array of vertices of the found QR code quadrangle
#straight qrcode
# if there is a QR code
if points is not None:
    # QR Code detected handling code

    print('Decoded data: ' + decodedText)
    nrOfPoints = len(points)
    print('Number of points:  ' + str(nrOfPoints))
    points = points[0]
    for i in range(len(points)):
        pt1 = [int(val) for val in points[i]]
        pt2 = [int(val) for val in points[(i + 1) % 4]]
        cv2.line(image, pt1, pt2, color=(255, 0, 0), thickness=3)
  
    print('Successfully saved')
    cv2.imshow('Detected QR code', image)
    cv2.imwrite('Generated/extractedQrcode.png', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print("QR code not detected")
    # display the image with lines
    # length of bounding box

My code worked very nice with an other pdf example but my goal is only to extract the pdf that i mentionned above.

Here an example of a pdf that worked fine with my code:

enter image description here

Note: I have noticed that the pdf file im scanning contains a selectable qrcode

enter image description here

0 Answers
Related