如何在(最好是纯净的)Python中解码QR码图像? [英] How to decode a QR-code image in (preferably pure) Python?
问题描述
TL; DR :我需要一种使用(最好是纯Python)从图像文件解码QR码的方法。
TL;DR: I need a way to decode a QR-code from an image file using (preferable pure) Python.
我有一个带有QR码的jpg文件,我想使用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 (此处的网站),据说只需提供以下路径即可解码图像中的二维码:
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
所以我只是使用 sudo pip install pyqrcode
进行安装。我对上面的示例代码感到奇怪的是,它仅导入 qrcode
(尽管不是 pyqrcode
)由于我认为 qrcode
是指此库它只能生成 qr码图像,这令我感到困惑。所以我用 pyqrcode
和 qrcode
尝试了上面的代码,但是都在第二行说 AttributeError:模块对象没有属性解码器
。此外,网站指的是Ubuntu 8.10(发布于6年前),我找不到它的公共(git或其他)存储库以检查最新提交。所以我进入下一个库:
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 (网站)声称是一个开放源代码软件套件,用于从各种来源(例如图像文件)读取条形码。
尝试在运行 sudo pip install zbar
的Mac OSX上安装它。此操作失败,并显示错误:命令 cc失败,退出状态为1
。我尝试在此答案的答案中提出建议问题,但我似乎无法解决。因此,我决定再次继续:
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 ,根据此博客文章可以使用以下代码轻松解码图像:
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()
所以我尝试使用 sudo pip install qrtools
安装它,但找不到任何东西。我还尝试了 python-qrtools
, qr-tools
, python-qrtools 和更多其他组合,但不幸的是无济于事。我想它是指此存储库,它说它基于ZBar(请参见上文)。尽管我想在Heroku上运行我的代码(因此更喜欢使用纯Python解决方案),但我已成功将其安装在Linux机器上(使用
sudo apt-get install python-qrtools
)并尝试运行它:
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`)
尽管这似乎可以对其进行解码,似乎做得不正确。此外,它需要ZBar,因此不是纯Python。因此,我决定再找一个图书馆。
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 (此处的网站)应该是流行的Java ZXing库的Python端口 a>,但最初且唯一的提交已使用6年,并且该项目没有自述文件或文档。
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.
对于其余的我发现了几个qr- en 编码器(不是 de 编码器)和一些可以为您解码的API端点。由于我不希望该服务依赖于其他API端点,因此我希望将解码保持在本地。
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.
有谁知道我如何用(最好是纯净的)Python从图像中解码QR码?欢迎所有提示!
So to conclude; would anybody know how I can decode QR-codes from images in (preferable pure) Python? All tips are welcome!
推荐答案
您可以尝试使用 qrtools $ c进行以下步骤和编码$ c>:
-
创建
qrcode
文件(如果尚未创建)现有
Create a
qrcode
file, if not already existing
- 我使用了
pyqrcode
为此,可以使用pip install pyqrcode
$ b $安装b -
然后使用代码:
- I used
pyqrcode
for doing this, which can be installed usingpip 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
- 安装
qrtools
使用sudo apt-get install python-qrtools
-
现在在python提示符下使用以下代码
- Install
qrtools
usingsudo 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
- 您可能需要使用
pip install pypng $ c $安装
PyPNG
c>使用pyqrcode
-
如果您有
PIL
安装后,您可能会得到IOError:解码器zip不可用
。在这种情况下,尝试使用以下方法卸载并重新安装PIL
p>
- You might need to install
PyPNG
usingpip install pypng
for usingpyqrcode
In case you have
PIL
installed, you might getIOError: decoder zip not available
. In that case, try uninstalling and reinstallingPIL
using:
pip uninstall PIL
pip install PIL
如果这不起作用,请尝试使用枕头
代替
pip uninstall PIL
pip install pillow
这篇关于如何在(最好是纯净的)Python中解码QR码图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!