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

后端 未结 5 1502
时光取名叫无心
时光取名叫无心 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 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
      

提交回复
热议问题