Generate and parse QR codes using python

Source: Internet
Author: User

Python generates QR codes. common modules include pyqrencode and qrcode.


1. Use the pyqrencode module to generate a QR code


Installing the pyqrencode module is complex and relies on many things. The following installation process is summarized after testing:


1. Download Cython: https://pypi.python.org/packages/source/C/Cython/Cython-0.19.1.tar.gz

Install: tar-zxf Cython-0.19.1.tar.gz & cd Cython-0.19.1 & python setup. py install


2. Download qrencode: http://fukuchi.org/works/qrencode/qrencode-3.4.2.tar.gz

Install yum install libpng-devel-y before installation

Installation: tar-zxf qrencode-3.4.2.tar.gz & cd qrencode-3.4.2 &./configure & make install


3, download Imaging: http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz

Install: tar-zxf Imaging-1.1.7.tar.gz & cd Imaging-1.1.7 & python setup. py install


4. Download: https://codeload.github.com/bitly/pyqrencode/zip/master

Installation:

Unzip-q pyqrencode-master.zip

Cd pyqrencode-master

Easy_install cython

Cython qrencode. pyx

Python setup. py install

Echo "/usr/local/lib">/etc/ld. so. conf

Ldconfig


5. Use pyqrencode to generate a QR code

>>> From qrencode import Encoder

>>> Encoder = Encoder ()

>>> Img = encoder. encode ("hello python! ", {'Width': 300 })

>>> Img. save ("char.png ")

Char.png is the generated QR code file.


Ii. Use the qrcode module to generate a QR code


1. Download: https://pypi.python.org/packages/source/q/qrcode/qrcode-2.0.tar.gz

2. Installation: tar-zxf qrcode-2.0.tar.gz & cd qrcode-2.0 & python setup. py install

3. Use qrcode to generate a QR code

>>> Import qrcode

>>> Q = qrcode. main. QRCode ()

>>> Q. add_data ("hello python! ")

>>> Q. make ()

>>> M = q. make_image ()

>>> M.save('char.png ')


3. QR code recognition


The zbar module is used to parse the QR code. The change module is directly installed in the exe file under windowns, which is relatively simple. There are many dependent packages in linux, so the installation process is not described. The usage is as follows:

def get_QR():    scanner = zbar.ImageScanner()    scanner.parse_config("enable")    pil = Image.open("char.png").convert('L')    width, height = pil.size    raw = pil.tostring()    image = zbar.Image(width, height, 'Y800', raw)    scanner.scan(image)    data = ''    for symbol in image:        data+=symbol.data    del(image)    return data

Data is two-dimensional content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.