Home  >  Article  >  Backend Development  >  Python parses QR code

Python parses QR code

高洛峰
高洛峰Original
2016-11-22 12:43:061931browse

Since the QR code image needs to be parsed, the operating environment is centos7.2 and the python version is 2.7.5. Since zbar only supports Python2.6, we are going to use source code compilation and installation. The specific steps are as follows:

1. Install the dependency package

yum install pdftk ImageMagick ImageMagick-devel ghostscript Python-imaging python-devel

2. Download the source code package at http://zbar.sourceforge.NET/download.html

3. The decompression method is:

tar   -jxvf    zbar-0.10.tar.bz2

4. Compile

./configure --without-gtk --without-qt --disable-video --prefix=/usr/local

5. Install

make && make install

6. Download the zbar source code and install it. The download address is https://pypi.python.org/pypi/zbar/0.10

7. Unzip the tar package

tar -zxvf zbar-0.10.tar.gz

8. Install

python setup.py  install

10. Determine whether the zbar package is installed successfully. If no error is reported, the installation is successful

Python parses QR code


11. Test zbar to parse the QR code image

# -*- coding:utf-8 -*-
import zbar
from PIL import Image

# 创建图片扫描对象
scanner = zbar.ImageScanner()
# 设置对象属性
scanner.parse_config('enable')

# 打开含有二维码的图片
img = Image.open(&#39;<你的图片路径>&#39;).convert(&#39;L&#39;)
#获取图片的尺寸
width, height = img.size

#建立zbar图片对象并扫描转换为字节信息
qrCode = zbar.Image(width, height, &#39;Y800&#39;, img.tobytes())
scanner.scan(qrCode)

data = &#39;&#39;
for s in qrCode:
    data += s.data

# 删除图片对象
del img

# 输出解码结果
print data


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn