How to decode a QR-code image in (preferably pure) Python?

后端 未结 5 1467
时光取名叫无心
时光取名叫无心 2020-12-02 04:10

TL;DR: I need a way to decode a QR-code from an image file using (preferable pure) Python.

I\'ve got a jpg file with a

相关标签:
5条回答
  • 2020-12-02 04:46

    I'm answering only the part of the question about zbar installation.

    I spent nearly half an hour a few hours to make it work on Windows + Python 2.7 64-bit, so here are additional notes to the accepted answer:

    • Download https://github.com/NaturalHistoryMuseum/ZBarWin64/releases/download/v0.10/zbar-0.10-cp27-none-win_amd64.whl

    • Install it with pip install zbar-0.10-cp27-none-win_amd64.whl

    • If Python reports an ImportError: DLL load failed: The specified module could not be found. when doing import zbar, then you will just need to install the Visual C++ Redistributable Packages for VS 2013 (I spent a lot of time here, trying to recompile unsuccessfully...)

    • Required too: libzbar64-0.dll must be in a folder which is in the PATH. In my case I copied it to "C:\Python27\libzbar64-0.dll" (which is in the PATH). If it still does not work, add this:

      import os
      os.environ['PATH'] += ';C:\\Python27' 
      import zbar
      

    PS: Making it work with Python 3.x is even more difficult: Compile zbar for Python 3.x.

    PS2: I just tested pyzbar with pip install pyzbar and it's MUCH easier, it works out-of-the-box (the only thing is you need to have VC Redist 2013 files installed). It is also recommended to use this library in this pyimagesearch.com article.

    0 讨论(0)
  • 2020-12-02 04:46

    There is a library called BoofCV which claims to better than ZBar and other libraries.
    Here are the steps to use that (any OS).

    Pre-requisites:

    • Ensure JDK 14+ is installed and set in $PATH
    • pip install pyboof

    Class to decode:

    import os
    import numpy as np
    import pyboof as pb
    
    pb.init_memmap() #Optional
    
    class QR_Extractor:
        # Src: github.com/lessthanoptimal/PyBoof/blob/master/examples/qrcode_detect.py
        def __init__(self):
            self.detector = pb.FactoryFiducial(np.uint8).qrcode()
        
        def extract(self, img_path):
            if not os.path.isfile(img_path):
                print('File not found:', img_path)
                return None
            image = pb.load_single_band(img_path, np.uint8)
            self.detector.detect(image)
            qr_codes = []
            for qr in self.detector.detections:
                qr_codes.append({
                    'text': qr.message,
                    'points': qr.bounds.convert_tuple()
                })
            return qr_codes
    

    Usage:

    qr_scanner = QR_Extractor()
    output = qr_scanner.extract('Your-Image.jpg')
    print(output)
    

    Tested and works on Python 3.8 (Windows & Ubuntu)

    0 讨论(0)
  • 2020-12-02 04:59

    The following code works fine with me:

    brew install zbar
    pip install pyqrcode
    pip install pyzbar
    

    For QR code image creation:

    import pyqrcode
    qr = pyqrcode.create("test1")
    qr.png("test1.png", scale=6)
    

    For QR code decoding:

    from PIL import Image
    from pyzbar.pyzbar import decode
    data = decode(Image.open('test1.png'))
    print(data)
    

    that prints the result:

    [Decoded(data=b'test1', type='QRCODE', rect=Rect(left=24, top=24, width=126, height=126), polygon=[Point(x=24, y=24), Point(x=24, y=150), Point(x=150, y=150), Point(x=150, y=24)])]
    
    0 讨论(0)
  • 2020-12-02 05:03

    For Windows using ZBar

    Pre-requisites:

    • Install ZBar by either:
      • Install Chocolatey and choco install zbar
      • Or use Windows Installer for ZBar
    • pip install pyzbar

    To decode:

    from PIL import Image
    from pyzbar import pyzbar
    
    img = Image.open('My-Image.jpg')
    output = pyzbar.decode(img)
    print(output)
    

    Alternatively, you can also try using ZBarLight by setting it up as mentioned here:
    https://pypi.org/project/zbarlight/

    0 讨论(0)
  • 2020-12-02 05:10

    You can try the following steps and code using qrtools:

    • Create a qrcode file, if not already existing

      • I used pyqrcode for doing this, which can be installed using pip install pyqrcode
      • And then use the code:

        >>> import pyqrcode
        >>> qr = pyqrcode.create("HORN O.K. PLEASE.")
        >>> qr.png("horn.png", scale=6)
        
    • Decode an existing qrcode file using qrtools

      • Install qrtools using sudo apt-get install python-qrtools
      • Now use the following code within your python prompt

        >>> import qrtools
        >>> qr = qrtools.QR()
        >>> qr.decode("horn.png")
        >>> print qr.data
        u'HORN O.K. PLEASE.'
        

    Here is the complete code in a single run:

    In [2]: import pyqrcode
    In [3]: qr = pyqrcode.create("HORN O.K. PLEASE.")
    In [4]: qr.png("horn.png", scale=6)
    In [5]: import qrtools
    In [6]: qr = qrtools.QR()
    In [7]: qr.decode("horn.png")
    Out[7]: True
    In [8]: print qr.data
    HORN O.K. PLEASE.
    

    Caveats

    • You might need to install PyPNG using pip install pypng for using pyqrcode
    • In case you have PIL installed, you might get IOError: decoder zip not available. In that case, try uninstalling and reinstalling PIL using:

      pip uninstall PIL
      pip install PIL
      
    • If that doesn't work, try using Pillow instead

      pip uninstall PIL
      pip install pillow
      
    0 讨论(0)
提交回复
热议问题