python qrcode解码,如何在(最好是纯净的)Python中解码QR码图像?

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 QR-code which I want to decode using Python. I've found a couple libraries which claim to do this:

PyQRCode (website here) which supposedly can decode qr codes from images by simply providing a path like this:

import sys, qrcode

d = qrcode.Decoder()

if d.decode('out.png'):

print 'result: ' + d.result

else:

print 'error: ' + d.error

So I simply installed it using sudo pip install pyqrcode. The thing I find strange about the example code above however, is that it only imports qrcode (and not pyqrcode though) Since I think qrcode refers to this library which can only generate qr-code images it kind of confused me. So I tried the code above with both pyqrcode and qrcode, but both fail at the second line saying AttributeError: 'module' object has no attribute 'Decoder'. Furthermore, the website refers to Ubuntu 8.10 (which came out more than 6 years ago) and I can't find a public (git or other) repository of it to check the latest commit. So I moved on to the next library:

ZBar (website here) claims to be "an open source software suite for reading bar codes from various sources, such as image files." So I tried installing it on Mac OSX running sudo pip install zbar. This fails with error: command 'cc' failed with exit status 1. I tried to suggestions in the answers to this SO question, but I can't seem to solve it. So I decided to move on again:

QRTools, which according to this blogpost can decode images easily by using the following code:

from qrtools import QR

myCode = QR(filename=u"/home/psutton/Documents/Python/qrcodes/qrcode.png")

if myCode.decode():

print myCode.data

print myCode.data_type

print myCode.data_to_string()

So I tried installing it using sudo pip install qrtools, which can't find anything. I also tried it with python-qrtools, qr-tools, python-qrtools and a couple more combinations, but unfortunately to no avail. I suppose it refers to this repo which says it is based on ZBar (see above). Although I want to run my code on Heroku (and thus prefer a pure Python solution) I successfully installed it on a Linux box (with sudo apt-get install python-qrtools) and tried running it:

from qrtools import QR

c = QR(filename='/home/kramer65/qrcode.jpg')

c.data # prints u'NULL'

c.data_type # prints u'text'

c.data_to_string() # prints '\xef\xbb\xbfNULL' where I expect an int (being `1234567890`)

Although this seems to decode it, It doesn't seem to do it correctly. It furthermore needs ZBar and is thus not pure Python. So I decided to find yet another library.

PyXing (website here) is supposedly a Python port of the popular Java ZXing library, but the initial and only commit is 6 years old and the project has no readme or documentation whatsoever.

For the rest I found a couple qr-encoders (not decoders) and some API endpoints which can decode for you. Since I don't like this service to be dependent on other API endpoints I would want to keep the decoding local though.

So to conclude; would anybody know how I can decode QR-codes from images in (preferable pure) Python? All tips are welcome!

解决方案

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
qrcode是一个Python开源库,用于生成二维码。它可以通过调用其提供的函数和方法来实现编码和解码二维码的功能。在Python使用qrcode库生成二维码非常简单。可以使用qrcode.make()函数来生成原始的二维码,也可以使用qrcode.QRCode类来生成带有图片的二维码。 要解码二维码,可以使用pyzbar库的decode()函数来实现。在解码之前,需要确保已经安装了pyzbar库,并且导入所需的模块。decode_qr_code函数可以用于解码给定路径下的二维码图片,同时可以指定只识别QR Code类型的二维码。 关于具体使用方法,可以使用import qrcode来导入qrcode库。编码时,可以使用qrcode.make()函数生成原始二维码,也可以使用qrcode.QRCode类来生成带有图片的二维码。 总结来说,qrcodePython用于生成和解码二维码的开源库。可以使用它来生成原始的二维码和带有图片的二维码,并且可以使用pyzbar库来解码二维码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [[python] Python二维码生成器qrcode库入门](https://blog.csdn.net/LuohenYJ/article/details/118941613)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python二维码应用(一)QRcode二维码生成&识别](https://blog.csdn.net/cungudafa/article/details/85871871)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值